dover said:
There are two situations under which I am not sure which one to use, pointer
or reference:
Passing parameter into a function
A object is aggregated in another one.
Is there any general rules of thumb about this?
Always use the weakest construction you can.
Use delegation unless you must inherit. Use 'double' unless you need 'float'
(the latter are more work for modern CPUs). Use private members without
Get() and Set() methods unless other classes have a legitimate reason to see
or change them. Put classes in the .cpp file unless you must share them
between other translation units.
Use references unless you must use a pointer. Pointers can re-seat, can
iterate an array, and can contain NULL, and cannot easily point to a
temporary.
That last point needs more explainations. Constant references can cause
invisible objects to exist.
The C++ keyword const implies (and sometimes enforces) a variable cannot
change its value. C++ functions can take arguments by copy, by address, or
by reference. Ideally, if an object passed into a function does not change,
the object should pass by copy:
void foo(SimCity aCity);
That code is inefficient. In general, programmers should not stress about
efficiency until they have enough code to measure it and find the slow
spots. In this situation, a more efficient implementation is equal cost.
When we pass by reference, our program spends no time making a huge copy of
an entire city:
void foo(SimCity &aCity);
Now if foo() won't change that city's value, the function should declare
that intention in its interface, using pass-by-constant-reference to
simulate pass-by-copy:
void foo(SimCity const &aCity);
That is the most efficient call syntax, cognitively and physically. It's
cognitively efficient because it gives foo() no copied object to foolishly
change and then discard. Statements inside foo() that might attempt to
change that city shouldn't compile. It's physically efficient because the
compiler produces opcodes that only give foo() a handle to an existing city,
without copying it.
C++ supports qualifications before their qualified types, such as "const
SimCity &". I try to write expressions with the most important part first.
There are also subtle technical reasons, in rare situations, to write
"SimCity const &", with the const after its type.
Now imagine if we call foo(1001). That works if SimCity has a constructor
that takes an integer:
class SimCity { public:
SimCity (int population);
....
};
Inside foo(), this works:
void foo(SimCity const &aCity)
{
Alien aAlien;
anAlien.abduct(aCity, 10);
}
Now, from what city did the alien abduct 10 citizens? Between foo(1001) and
aCity is an invisible temporary object. Only constant references can summon
such cities, out of thin air.
Spooky.