Jump to content
  • Advertisement

Archived

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

Feldi

pointer, reference, what?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
GameProgrammer and Endurion, thanks for your answers. Very helpfull!
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 this post


Link to post
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 version

S2 = 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 this post


Link to post
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&).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!