Operator overloading

1
2
3
4
5
6
CSprite CSprite::operator * (double scale)
{
// code to scale the size of the sprite and make a new one based on that size.

  return newSprite;
}



1
2
3
4
bool CSprite::operator > (const double& area) const
{
	return ((height*width) > area;
}


Now, I am not fallowing why they have different parameter descriptions. Both return same results, except one has weird syntax, other not.

By weird syntax, do you mean const T& ? Well, that bit is really not needed in here.. Anyway, passing by reference (&) works a lot like passing a pointer (*) but has the syntax of passing by value. The const means that the object will not be modified. Passing a thing as a const reference is the preferred way for large objects that don't need to be modified.
Last edited on
The general approach is to use const & when you can so that you forbid any unwanted changes to your arguments. On the other hand built-in arguments often are passed by value for other reason also. So both approaches work fine. In these examples there isn't any particular advantage I think. But I would use the same approach if there wasn't any special reason to do differently.
Topic archived. No new replies allowed.