Pointer and reference

D

dover

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?

Many thanks!
 
P

Phlip

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.
 
J

JKop

dover posted:
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?

Many thanks!


Yes there is: Use a reference.


Upon failure of that method, use a pointer.


void Cheese(int& k)
{
k *= 3 + k;
}


void Squeeze(char* kra)
{
kra[0] = 'K';
kra[7] = 't';
}

void Squeeze(char& kra)
{
(&kra)[0] = 'K';
(&kra)[7] = 't';
}

int main()
{
int monkey = 7;

Cheese(monkey);

char blah[] = "Chocolate squirrel";

Squeeze(blah)

Squeeze(blah[0]);
}


Obviously, for "Cheese", the reference is what you want.

For "Squeeze", you'll want a pointer, although a reference
will also do the job, just not as eloquently.


-JKop
 
V

Victor Bazarov

Phlip said:
[...]
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);

Curiously, aliens should actually abduct copies, otherwise [logically]
the 'aCity' has to be non-const if we expect its population to decrease
by 10 as a result of alien abduction.

Just a thought...
}

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.

There is still a big question whether the aliens did in fact abduct any
citizens. If the temporary object is const inside 'foo', its population
hasn't really changed. Yet a local alien managed to get away with ten
of something. That's spooky.

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top