Jump to content
  • Advertisement

Archived

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

3flares

question about passing vaules ?

This topic is 5676 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

whats difference between passing by reference and passing a pointer to an object like a class. it is faster, smarter, is it just the same, or for cleaner code, I wander ... ? please help.

Share this post


Link to post
Share on other sites
Advertisement
Syntactically the difference is this. Passing a variable via a pointer means that you'll be working with a variable that represents the address in memory of the variable you passed. This has the side effect that you'll have to dereference the pointer (i.e. use the * operator) anytime you need to use or modify the data at said address. A reference on the other is an alias for the variable you passed. That is the function argument becomes another name for the variable you passed only within the functions scope. So no need to dereference it. You use it like a regular variable. Passing a variable using a pointer is a carry-over from C. So most people will say use a reference if you're using C++. Heck. That's why they are there. As far as the mechanics are concerned, I believe they pretty much work the same, somebody correct me if I'm wrong, please, so yeah, passing by reference looks and acts a lot cleaner. I hope that helps. Sorry to ramble but I'm half asleep. Later.

Edit:
Oh yeah. There are sometimes when you'll have to use a pointer (I believe) such as when passing the starting address of an array or C style string.

[edited by - microdot on January 31, 2003 7:44:43 PM]

Share this post


Link to post
Share on other sites
a reference cannot be NULL, whereas a pointer can.

so, you can pass a NULL pointer to:
void Somefunc(int*); 

but not:
void Somefunc(int&); 


which may or may not matter depending on how you use them. if you do use a pointer, you should check if it is NULL before dereferencing it within the function; if you use a reference the compiler will not allow a NULL to be passed in the first place.

other than that, they are the same.

Share this post


Link to post
Share on other sites
as a hint ... when about the C language, it only supports one type of parameter passing, call-by-value ... but when you read old C programming books they say that you can do two types of paramter passing, call-by-value, and call-by-reference ... and then they go on to show you how to do call-by-reference, you pass the address of the object, and accept a pointer in the function. SO ... passing a pointer is passing the object by reference (it''s address). Then C++ added the reference mechanism, to make the syntax simpler ... AND so places that do not need dynamic memory allocation, nor NULL pointer objects, can give visual clues about their design limitations ...

Machine code wise the two are identical ...

Also note, even though it is an ERROR to pass NULL to a function expecting a reference ... that doesn''t mean it can''t happen ... so if you ever get a stupid error you can''t figure out, when all your doing is using the reference parameter ... you know have a clue ... here''s an example where it could happen and it is the programmer''s job to prevent it.


  
Object *GetNextObject(void)
{
if(thereIsAnotherObject)
return objects[currObject++];
else
return NULL;
}
void DoSomethingToObject(Object &object)
{
object.DoSomething();
}
int main(void)
{
Object *object = GetNextObject();
DoSomthingToObject(*object);
}


this function would crash ... whenever there is no object to return ... which is to be expected, because you cannot call a function on a NULL pointer ... but if you looked just at the place where it crashed (DoSomething ...) then you would not find any possible error at all ... because there is none ... it is the caller of DoSomething who is violating the sacred rule ... NEVER DEREFENCE NULL, but he doesn''t know he''s doing it, because the crash it happening elsewhere in the program ... what''s REALLY bad ... is it could be MANY MANY functions later that the crash occurs ...

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!