Connecting Tech Pros Worldwide Help | Site Map

Pointer and reference

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:36 PM
dover
Guest
 
Posts: n/a
Default Pointer and reference

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!



  #2  
Old July 22nd, 2005, 04:36 PM
Phlip
Guest
 
Posts: n/a
Default Re: Pointer and reference

dover wrote:
[color=blue]
> There are two situations under which I am not sure which one to use,[/color]
pointer[color=blue]
> or reference:
>
> Passing parameter into a function
> A object is aggregated in another one.
>
> Is there any general rules of thumb about this?[/color]

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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


  #3  
Old July 22nd, 2005, 04:36 PM
JKop
Guest
 
Posts: n/a
Default Re: Pointer and reference

dover posted:
[color=blue]
> There are two situations under which I am not sure which[/color]
one to use,[color=blue]
> 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![/color]


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
  #4  
Old July 22nd, 2005, 04:36 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Pointer and reference

"Phlip" <phlip_cpp@yahoo.com> wrote...[color=blue]
> [...]
> 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);[/color]

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...
[color=blue]
> }
>
> Now, from what city did the alien abduct 10 citizens? Between foo(1001)[/color]
and[color=blue]
> aCity is an invisible temporary object. Only constant references can[/color]
summon[color=blue]
> such cities, out of thin air.
>
> Spooky.[/color]

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.

:-)


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.