Jump to content
  • Advertisement
Sign in to follow this  
Structure

In this case is passing by reference faster?

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

Hi, Ive got a fixed point wrapper class that ive been using for a while now. The class is simply Class Fixed { int value; //all overloaded methods etc. } Now my question is, is it quicker to pass this class by reference or by value? If its by reference then the pointer must be dereferenced , but if its by value then a temp object must be constructed. As the data is only 32 bits wide im having trouble working out what is actually faster, any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Ok thanks!

I was kinda leaning towards that as it didn’t seem to make sense to pass over the pointer value then have to dereference it when the data needed could be there already if passed by value.

Share this post


Link to post
Share on other sites
What about in the case of the = operator?

Is this

inline Fixed Fixed::operator=(const Fixed x)
{
return Fixed16(x);
}

Or this faster?

inline Fixed& Fixed::operator=(const Fixed x)
{
m_v = x.m_v;
return *this;
}

Share this post


Link to post
Share on other sites
sorry a moment of confusion here that shouldn’t be

return Fixed16

but

return Fixed

Or are you talking about something else?

Share this post


Link to post
Share on other sites
Quote:

inline Fixed Fixed::operator=(const Fixed x)
{
return Fixed16(x);
}


that will never change the object you're assigning to so if you did:
a = b;//a will still hav its old value!

that's what Im saying is a tad confusing

Share this post


Link to post
Share on other sites
Ahh really…


I think I have a misconception on how that works then.


Why does the operator need to return a value at all then? I thought it was the returned value that was assigned.

Share this post


Link to post
Share on other sites
Quote:
Original post by Structure
Ahh really…


I think I have a misconception on how that works then.


Why does the operator need to return a value at all then? I thought it was the returned value that was assigned.


It doesn't, any assignment work should be done *inside* op=, the reason for returning *this is so that you easily can chain calls i.e. to enable:
a = b = c; to work as expected.

Share this post


Link to post
Share on other sites
Quote:
Original post by Structure
Or are you talking about something else?


He's saying that the first example is "broken" since it doesn't actually do an assignment. Also, neither checks for self assignment, though I guess it wouldn't matter in this case and the compiler would optimize it away anyway.

As far as speed in this case, both functions (once the first once is fixed) will probably be optimized into exactly the same result.

One more thing. Here is the canonical form of operator=():
    Fixed & Fixed::operator=( Fixed const & x )
{
if ( this != &x )
{
...
}
return *this;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!