• Popular Now

• 9
• 9
• 10
• 9
• 10

Archived

This topic is now archived and is closed to further replies.

pointer, reference, what?

This topic is 5295 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

these two things it seems to do the same but why would be the 1st method be preffered over the 2nd one, (if it would) void Point::Set(const Point &p) { x=p.x; y=p.y; } void Point::Set(Point *p) { x=p->x; y=p->y; }

Share on other sites
In the second case p (pointer) could be NULL, causing an access violation, whereas in the first case p (reference) will always be valid (unless you do some ugly pointer dereferencing).

Use references when you always expect a valid parameter, use pointers, when you need some "invalid" parameter value.

Share on other sites
ic, thanks.
Would there also be any effect on speed?

Share on other sites
Speedwise they should be the same, nowadays both will be 32-bit wide. It''s just a lot better to pass a ref or a pointer than to copy a huge object.

Share on other sites
Internally references are pointers so the two examples generate identical assembly/hexcode output. It's just the C++ that's different, and as Endurion said, references are always assigned to something valid. And in case you were wondering, the reason you pass "Point &p" instead of "Point p" is because everything passed to functions needs to be pushed on and off the stack, which isn't very fast, so you want to reduce the memory to push/pop. A reference/pointer is 4 bytes (32 bits) but the actual value being passed if you don't use a reference/pointer can be much larger... Point is probably 8 bytes, so it's a slight savings.

If you're passing something 4 bytes or [especially] less, and it's only used for input, you do not want to pass it by reference/pointer.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

[edited by - CGameProgrammer on October 21, 2003 6:22:43 AM]

Share on other sites
Use a pointer when you need to pass an array.

Share on other sites
Abdulla, I knew how and why to use pointers, but I just
had no deeper understanding why would one use referencing
instead of pointers, but now I do.

Share on other sites
One other thing: copy constructors and assignment operators have to take references as arguments, IIRC.

Share on other sites
quote:
Original post by sbennett
One other thing: copy constructors and assignment operators have to take references as arguments, IIRC.

No, you could just as easily make a copy constructor using a pointer... example

struct SomeStruct{ int x; operator =(const SomeStruct *p) {  x = p->x;//Could also do:  x = *p.x; } operator =(const SomeStruct &p) {  x = p.x; }};SomeStruct S1, S2;S1.x = 10;S2 = &S1; //Assign with the pointer versionS2 = S1;  //Assign using a reference

This is done just the same as it is when calling functions with pointers, you must take it''s address first.

Share on other sites
quote:
No, you could just as easily make a copy constructor using a pointer...

No, that would be a constructor (even if it makes a copy), not a copy constructor:
struct A{  A(const A*)  {  }};..A a;A b(a);

b(a) would still call a compiler generated copy ctor: A(const A&). The same goes for the default assignment operator: A& operator=(const A&).