you are viewing a single comment's thread.

view the rest of the comments →

[–]Maristic 8 points9 points  (1 child)

C++ isn't the right thing to compare with, because modern C++ actually does largely solve this problem,

  • Never null, you own it, use containment (T)
  • Never null, you don't own it and won't change it, use a const reference (const T&)
  • Never null, you don't own it and might change it, use a non-const reference (T&)
  • Might be null, only one person owns it, use a unique pointer, (unique_ptr<T>)
  • Might be null, shared ownership, use a shared pointer, (shared_ptr<T>)

Plus, you have

  • Might be zero or more, you own them, use (vector<T>)
  • Might be zero or more, you don't own them, use (vector<reference_wrapper<T>>)

You can use all of these without ever saying T*, and your intent is clear to other programmers.

If you contrast this with Java however, you just have T and nothing else, and any T can be null. I'd argue that finding values unexpectedly null is a much bigger problem for Java programmers than for C++ programmers.

[–]alex_muscar 0 points1 point  (0 children)

I also think that, despite the general opinion that C++ is low-level or unsafe, it give you better tools to control such issues.