Jump to content
  • Advertisement

Archived

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

HellStorm

C++ stuff

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

Hey, could someone tell me what the advantages are from passing a reference to a large class within a function as opposed to using pointers example: void dosumthin(sumclass & myclass) { //do whatever } //versus void dosumthin(sumclass * myclass) { //same } thanx

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
1. You don''t have to check whether the pointer is NULL or invalid within the called function. Saves you a check.

2. The caller must send a valid object reference. By using references, you can enforce this at compile time.

Note that when sending a pointer, you might in fact want to treat it as an array. You can''t do that with just references.

BTW, if you aren''t going to modify the object within the called function, you should specify it as a const reference.

void dosumthin(const sumclass& myclass)
{
...
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I read in my c++ book that when u use references instead of pointers that it was faster in the cases of classes because the class object did not have to be copied, thus when modifying the data it modified the class directly. Is this true?.

Share this post


Link to post
Share on other sites
The & and * operators work to together to reference and dereference pointers. The only difference is that you use & to pass the address of a pointer, whereas you use * to create a pointer. Therefore there is no difference in speed. If you use classes, the class object is not copied in either case.

--Antz
Quanta
http://quanta.virtualave.net

Share this post


Link to post
Share on other sites
Be VERY carefull with references though! If the type/class isn''t the EXCACT same asin the defenition of the function then an anonumys variablewill be created by the compiler wich isn''t at all what we wan''t. Not at all. So exceptfor that, there is no realdifference in speed or so, it is more or less a matter of taste.

Share this post


Link to post
Share on other sites
Right. Then you need to use the dynamic_cast operator to get it all correct.

And, if I''m not mistaken, there is no difference in speed since references are just pointers made earier to deal with in a notational and syntactical sense. I hate ''em. =) Just use pointer for real while non-assembly languages still have them. =)

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!