Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Differences between Variables, References and Pointers (C++)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
nybbles
n00b
n00b


Joined: 10 May 2003
Posts: 55
Location: Vancouver

PostPosted: Sun May 25, 2003 6:10 pm    Post subject: Differences between Variables, References and Pointers (C++) Reply with quote

Okay so I'm kinda new to C++... everything so far isnt so bad, apart from being much more convulated than Java. But there is one concept (or maybe two?) that consistently stump me.

Right... here goes.

A variable is a variable. It's just location in memory holding some value.
A pointer is a variable, except it does not hold a value. It holds the memory address to a variable. We use the indirection operator to get that variable, then we can output its value.

Now... a reference, I don't know. Where does the reference lie between a pointer and a normal variable? Also, whats the difference in OOP when returning a Class variable like so

Code:
Class returningNormalVariable();


and returning a Class reference, like so?

Code:
Class &returningReference();


and lastly, is this even valid, and how does it differ from the above?

Code:
&Class returningWhatever();


please help! My brain is being twisted and contorted by this madness...
_________________
oo oo aah aah
Back to top
View user's profile Send private message
Goalie_Ca
Apprentice
Apprentice


Joined: 13 Jan 2003
Posts: 156
Location: Vancouver,B.C

PostPosted: Sun May 25, 2003 6:35 pm    Post subject: Reply with quote

It helps to think like an assembly programmer. Essentially, all those * and & tell the computer is how to interpret the address.

lets let the label var = some address where the variable is to be stored.

var=2
Tell's the computer to store 2 at the memory location where var address'

*var=2
now, you are saying to store 2 where the value of var points to. (ie: not the value of the label, but the address is the value of the memory location where the label points)

&var
tells you the actual value of the label var. ie: the address where var is stored.

With arrays you can address them several ways:
char *p;
*(p+2) == p[2];
p = address of first element in array
_________________
Jabber: goalieca[AT]jabber.fr
Beautiful Vancouver, B.C.
http://www.sfu.ca/~rdickie/images/sig_small.jpg
Back to top
View user's profile Send private message
nybbles
n00b
n00b


Joined: 10 May 2003
Posts: 55
Location: Vancouver

PostPosted: Sun May 25, 2003 11:32 pm    Post subject: hmmm... what about objects Reply with quote

Please answer these questions toooo!!!
Quote:
Also, whats the difference in OOP when returning a Class variable like so

Code:
Class returningNormalVariable();



and returning a Class reference, like so?

Code:
Class &returningReference();



and lastly, is this even valid, and how does it differ from the above?

Code:
&Class returningWhatever();


please help! My brain is being twisted and contorted by this madness.

_________________
oo oo aah aah
Back to top
View user's profile Send private message
maccullt
n00b
n00b


Joined: 05 Nov 2002
Posts: 2
Location: ottawa, on, canada

PostPosted: Mon May 26, 2003 4:46 am    Post subject: Reply with quote

References allow you to have pointer-like behaviour without pointer syntax. In many ways they are a syntactic sugar that makes it easy to write natural looking operator overloading.

Code:
SomeClass someFunc()


someFunc returns an object of type SomeClass

Code:
SomeClass& someFunc2()

someFunc2 returns a reference to an existing object of type SomeClass.

The interesting question for someFunc2 is where does this object I'm referring to live? Was it a global, etc?

I'm not sure what you mean in your last question:
Code:
&ClassreturningWhatever();


Without more context it looks like you're taking the address of a function called ClassreturningWhatever.

Aside from the operator overloading idioms, I use references when I want reference (as opposed to value) semantics and I don't want to deal with the issues of NULL pointers or who owns the object. E.g.
Code:
void SomeFunc( const SomeClass& );

The above declaration tell you I need a access to an object of type SomeClass. It has to exist, I'll only be using it's const interface, etc. If I had written it as:
Code:
void SomeFunc( SomeClass* );

It's more ambiguous. Does SomeFunc handle nulls. Will it free the pointer, etc.
Back to top
View user's profile Send private message
Proton
Apprentice
Apprentice


Joined: 16 Mar 2003
Posts: 195
Location: Estoril, Portugal

PostPosted: Mon May 26, 2003 5:38 am    Post subject: Reply with quote

Yes, references are great, they are trouble-free pointer arguments or returns. Basically, you can do a whole program passing your classes by value and then try to optimize it simply by putting a few "&" in the arguments and return values!

But this is important! Anything that is passed by reference MUST have a memory address, just like if it was a pointer. So, if you have

Code:

class GiganticInt { ... };

void setSomething(GiganticInt &i) { ... }
GiganticInt &returnFromSomething() {...}


you can do:

Code:

GiganticInt a;

setSomething(a);
a = returnFromSomething();


but NOT:

Code:

setSomething(a + 1);  // <-- This is wrong


Happy C++ programming!
_________________
Sérgio @ Portugal
IST - LEIC
Back to top
View user's profile Send private message
fragbert
Tux's lil' helper
Tux's lil' helper


Joined: 18 Apr 2003
Posts: 75
Location: Dallas, TX

PostPosted: Tue May 27, 2003 9:21 am    Post subject: Reply with quote

A reference is basically just an alias for an object. When you pass by reference, you are essentially granting the called procedure the use of your variable by a new name (the one in the function declaration). Returning by reference is similar, but you must be careful not to return a reference to a local variable, since the variable is destroyed once the function exits.

References are different than pointers, in that pointers have associated operations (*, ->, etc), and references do not. For more information on pointers, references, etc, I highly recommend "C++ FAQs" by Cline, Lomow, and Girou. It is an invaluable reference (no pun intended :P) on all things in C++.

Proton wrote:
but NOT:

Code:

setSomething(a + 1);  // <-- This is wrong


No, this may be correct. It does, however, depend on correct implementation of "GiganticInt::operator+()", which should return a GiganticInt by value. ie:

Code:
GiganticInt GiganticInt::operator+(int x) { ... }




Michael
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum