In C, when a parameter, struct member, or any variable needs to be "optional" - that is, it may either have a value or not have a value, the solution was to use a pointer. If the pointer was null, the value did not exist. If the pointer is not null, dereferencing it gives you the value. In modern C++, there will eventually be the std::optional class (nearly identical to boost::optional) which encapsulates this behavior in a safe way. It's not in C++ yet, unfortunately, but you can use Boost or replicate it yourself until then. http://www.lb-stuff.com/pointers |
Please try to understand the difference between value semantics and reference semantics. std::experimental::optional<T> holds an optional (non-polymorphic) value of type T. "A program that necessitates the instantiation of template optional for an lvalue reference or rvalue reference type ... is ill-formed." The grotesque std::experimental::optional< std::reference_wrapper<T> > over the straightforward T*, anyone? http://www.cplusplus.com/forum/beginner/166841/#msg839293 |
'The World’s Dumbest Smart Pointer' has finally made it into the Library Fundamentals 2 TS. std::experimental::observer_ptr<T> is a sane drop-in replacement "for near-trivial uses of bare/native/raw/builtin/dumb C++ pointers." http://www.cplusplus.com/forum/beginner/166841/#msg839434 |
when you want to reach any member of the array you can always read it with a[i] so why use pointer and access it with *(p+i)? |
In fact, *(p+i) is essentially just syntactic sugar for p[i]. |
Standard wrote: |
---|
The expression E1[E2] is identical (by definition) to *((E1)+(E2)) |