Jump to content
  • Advertisement
Sign in to follow this  
bigdilliams

Small question about functions

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

Hello forum,
here's my question:

What does the "const" at the end of a function do?


bool CheckAnimation(........) const;

const FrameSkeleton& GetSkeleton() const
{
return ........;
}

Share this post


Link to post
Share on other sites
Advertisement
In C++, it applies to member functions and specifies that the function does not modify the object with which it is called. You can only call const-functions on a const object, and you cannot call non-const functions on a const object.

Share this post


Link to post
Share on other sites

You can only call const-functions on a const object

That's not right. You probably meant to say something else...

Share this post


Link to post
Share on other sites
Member functions are essentially functions that take an implicit first parameter (called `this'), which is a pointer to the object on which they are called. A const member function is one in which the `this' pointer is const.

Share this post


Link to post
Share on other sites

[quote name='Brother Bob' timestamp='1353166388' post='5001769']
You can only call const-functions on a const object

That's not right. You probably meant to say something else...
[/quote]
No, I meant what I wrote, but I see now how my statement can be read in two ways that have different meanings. You can call anything on a non-const object, but only const-functions on a const object.

Share this post


Link to post
Share on other sites
Oh, I can now see the other interpretation. I had tried before and failed. Now the sentence is strange because it is the conjunction of two clauses that mean the same thing. That's probably why my brain kept refusing to see what you meant.

Share this post


Link to post
Share on other sites

You can call anything on a non-const object, but only const-functions on a const object.

Well to be pedantic, there are also volatile functions and objects (and const volatile functions and objects). If an object is non-const, but volatile, you can't call a const-function that isn't also a volatile function on it. And if you've got a const object you can also call a const and volatile function on it. Fortunately, volatile and const volatile functions are very rare in the wild.

Share this post


Link to post
Share on other sites
bigdilliams: the other guys already answered what it does, but I wanted to add when and why to use it. Basically, any time you know the function should not modify the object, make it const. Then when you accidentally try to modify one of those objects, the compiler will catch it and you get a clear error message instead of a painful debug session. The const labels will also help when reading the code and reasoning about it.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!