PDA

View Full Version : pointer/reference



mickey
15th July 2006, 20:22
hi, what is better? which different? thanks


OBJ o;
OBJ* pointer = o;
this->pointer->....
// OR
OBJ& get();
this->get().
//or
OBJ* get();
this->get()->.....

Methedrine
15th July 2006, 20:49
Basically a reference and a pointer are pretty similar as they both point to a memory segment, the main difference, however, is that references must be initialised to reference a variable and therefore can only be NULL when forced to be null (pointers may not be initialised and be NULL instead).

Also, your first example is wrong, it must be:


OBJ o;
OBJ *pointer = &o;


&o returns the address of o which must be stored inside the pointer.

aMan
15th July 2006, 23:12
OBJ* get();
this->get()->.....

with this code you have a problem with

get()
then a object would be createt, but you wouldn't be able to access it. this would waste some memory..

regards..
aman..

jacek
15th July 2006, 23:16
Use pointers when you have to and references when you can use them.

mickey
16th July 2006, 13:09
OBJ* get();
this->get()->.....

with this code you have a problem with

get()
then a object would be createt, but you wouldn't be able to access it. this would waste some memory..

sorry what's problem with get? it return a pointer to my object; it's an interface to access 'o' for other object... o is a private menber...

mickey
16th July 2006, 13:11
Use pointers when you have to and references when you can use them.
Use? My problem is; how do I have to declare it? which is likely? Before this I know yet I'll must use o; otherwise I don't wonder it..not?

aMan
16th July 2006, 14:42
sorry what's problem with get? it return a pointer to my object; it's an interface to access 'o' for other object... o is a private menber...
oh, then it's ok..


@references
i assume he is thinking of the following:

int blub(int& a, int& b);

see here (http://www.cplusplus.com/doc/tutorial/functions2.html) for details..

regards..
aman..

jacek
16th July 2006, 14:54
Use? My problem is; how do I have to declare it? which is likely? Before this I know yet I'll must use o; otherwise I don't wonder it..not?
In this case use == declare.