*pointer or &ref -- whats the diff

Started by
9 comments, last by GameDev.net 18 years, 8 months ago
tell me do i have this concept correct.. a pointer ( int * pInt ) holds the address of a variable.. a refrence ( int & rInt ) holds the address of a variable.. so they are the same? if not- maybe a small example of when to use each?
Advertisement
Yep, but a reference cannot be NULL (unless you do some ugly things like ptr=NULL; func(*ptr);).
Praise the alternative.
They're not the same, and it's a "deficiency" of C++.

Fundamentally, pointers and references encapsulate the same quantity: the address of a variable. Practically, however, a pointer exposes that address to manipulation by the programmer while a reference only exposes the value pointed to. You can think of a reference as a compiler-managed pointer, meaning that you should use it unless you specifically need access to the address itself.
What Oluseyi just said using big words is: you can't make a reference point to something else. That's the other big difference.

There are a lot of situations where you could go either way. The basic style guideline is: use references whenever you can, use pointers when you need to.
okay so say i have 2 pointers to some object
and i want to compare the addresses of the object from the pointers...
int pooey = 88;int* num1 = pooey;int* num2 = pooey;//now i want to compare the//address of what is pointed to if(num1 == num2)if(*num1 == *num2)if(&num1 == &num2)if(*&num1 == *&num2)//mmm.. which should i use//or should i int* num1 = &pooey;int* num2 = &pooey;


thanks

---------------------- ;P

int* num1 = &pooeyint* num2 = &pooeyif(num1 == num2)


will check if they are pointing at the same thing. Now if you wanted to compare the values they are pointing to, that would be different code.

Also, when I was first learning C, the different usages of the * and & symbols confused me for a while. Just remember: * and & mean very different things when they are used on a value versus when they are used on a type.
int pooey = 88;      // Fine// Both lines, same problem:// num1 is a pointer which stores and address. The = operator sets the address.// If you want the address to be 88, this is find but will need a cast. If you// want the address to be that of pooey, you want to change it to num1 = &pooeyint* num1 = pooey;int* num2 = pooey;// Compares addresses. Fine but may not be what you wan't.if(num1 == num2)// Compares values. Fine.if(*num1 == *num2)// Compares addresses, but the addresses compared are that of num1 and num2,// not the addresses STORED by num1 and num2. Again, legal, but probably// not what you want.if(&num1 == &num2)// For ints, this is the same as the first if statement. Compares addresses.if(*&num1 == *&num2)

No references here at all. I was going to write whether each statement evaluated to true or false but since your initial assingments are a bit odd, it probably wont help much.
I'm in the process of using references for just that reason... safety. However one thing that Im working on is a couple functions that send various objects via their common base, albiet with only the base variables/functions accessable. Access in this way seems to work fine inside the function itself (with some generous help from others here). But I have not yet tried to send a reference this way as a parameter.

ex: multiple "car::ford", "truck::gmc", and "motorcycle::suzuki" objects (all derived from "vehicle::") in a . Instead of passing "car::ford" pass "vehicle::ford". The idea being that a single function can send any derived object... including new ones that I add later.

Just curious if its possible. Or if I should stick with pointers in this case. which work in this way I believe.
References also behave polymorphically. A reference to a base class could very well actually refer to a derived class, and will behave just as it is was a pointer. All virtual functions will be dispatched to the proper dynamic type of the reference.
if(*num1 == *num2)

That is probably going to crash your program since chances are 88 doesnt point to a valid page in memory.

You probably want:
int* num1=&pooey

The & operator gives you the address of something. A pointer holds that address.

This topic is closed to new replies.

Advertisement