Because sometimes things cannot be overestimated. (For example, a link to Singleton.)
Because it's great in a function to know that your argument cannot be null.
But mainly because it allows you to use what is really a pointer, but which acts as a local value object. C ++ is trying hard to quote Stroustrup to make instances of the "do as ints d" class. Passing int by vaue is cheap because int is placed in a machine register. Classes are often larger than ints, and passing them at cost has significant overhead.
The ability to pass a pointer (which is often the size of an int or, possibly, two ints) that "looks", an object of value allows us to write cleaner code without "detailing the implementation" of the differences. And, along with operator overloading, it allows us to write classes using syntax similar to the syntax used with ints. In particular, this allows us to write template classes with syntax that can be equally applicable to primitive ones, such as ints and classes (for example, the Complex number class).
And, especially with operator overloading, there are places, we must return the object, but again, it is much cheaper to return the pointer. Oncve again, returning the link, our "exit".
And pointers are complicated. Not for you, maybe, not for those who implement the pointer, it's just the value of the memory address. But, remembering my CS 101 class, they defeated several students.
char* p = s; *p = *s; *p++ = *s++; i = ++*p;
can be confusing.
Damn, after 40 years C, people still cannot even agree if there should be a pointer declaration:
char* p;
or
char *p;
tpdi Apr 08 '09 at 2:21 2009-04-08 02:21
source share