Jump to content
  • Advertisement
Sign in to follow this  
andrew1b

Const definition for C++ functions

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

What's the difference between:
GS_VECTOR3 operator + (const GS_VECTOR3& v3);
and
GS_VECTOR3 operator + (const GS_VECTOR3& v3) const;
? Why is that const for?

Share this post


Link to post
Share on other sites
Advertisement
The const on the end of the definitions means the function doesn't modify any of the fields in the function's class. In your first function, it's possible for the function to modify your x, y, z coordinates (I assume that's what's in GS_VECTOR3). However the second function guarantees that nothing is changed.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xycaleth
The const on the end of the definitions means the function doesn't modify any of the fields in the function's class. In your first function, it's possible for the function to modify your x, y, z coordinates (I assume that's what's in GS_VECTOR3). However the second function guarantees that nothing is changed.



You didn't ask about the const on the function argument, so I assume you understand the purpose of that: Namely, it means that the function is not going to do anything that changes the argument. But what is meant by change? Obviously, operator + cannot be written as follows:


GS_VECTOR3 operator + (const GS_VECTOR3& v3)
{
v3.x = 4;
}



because v3 is const. But what about the following?


GS_VECTOR3 operator + (const GS_VECTOR3& v3)
{
v3.foo();
}




Is this possible? The answer is it depends. If foo() is a const function, it's possible because that means that internally, foo() won't set any of its members either, nor will it call other functions that set its members.

So you have the following relationship:

1) If a variable is const, you can only call const member functions on it and read the value of member data.
2) If a variable is not const, you can do anything to it.

3) If a member function is const, it can be called on any type of variable, const or non-const.
4) If a member function is not const, it can only be called on const objects.


In your original example operator+ is a member function. So we apply rule 3) seeing that it can be called on any type of object.



Maybe the easiest way to understand is consider what happens if that const is not there (your first example):


class GS_VECTOR
{
public:
GS_VECTOR operator + (const GS_VECTOR& v3)
{
...
}
};

void do_something(const GS_VECTOR& v1, const GS_VECTOR& v2)
{
GS_VECTOR v3 = v1 + v2; //Doesn't compile
}



It fails to compile because v1 is const, meaning that only const member functions can be called. But operator+ is not const, so compilation fails.


If you write a function that does not change the value of any of the class's members, you should always consider making it const.

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!