Summary
Pointers in C++ are almost identical to
pointers in C, which is good. Otherwise, a lot of C code wouldn’t compile
properly under C++. The only compile-time errors you will produce occur with
dangerous assignments. If these are in fact what are intended, the compile-time
errors can be removed with a simple (and explicit!) cast.
C++ also adds the reference from
Algol and Pascal, which is like a constant pointer that is automatically
dereferenced by the compiler. A reference holds an address, but you treat it
like an object. References are essential for clean syntax with operator
overloading (the subject of the next chapter), but they also add syntactic
convenience for passing and returning objects for ordinary
functions.
The copy-constructor takes a reference to
an existing object of the same type as its argument, and it is used to create a
new object from an existing one. The compiler automatically calls the
copy-constructor when you pass or return an object by value. Although the
compiler will automatically create a copy-constructor for you, if you think one
will be needed for your class, you should always define it yourself to ensure
that the proper behavior occurs. If you don’t want the object passed or
returned by value, you should create a private
copy-constructor.
Pointers-to-members have the same
functionality as ordinary pointers: You can choose a particular region of
storage (data or function) at runtime. Pointers-to-members just happen to work
with class members instead of with global data or functions. You get the
programming flexibility that allows you to change behavior at
runtime.