This topic is 1884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello forum,
here's my question:

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

[CODE]
bool CheckAnimation(........) const;

const FrameSkeleton& GetSkeleton() const
{
return ........;
}
[/CODE]

Share on other sites
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 on other sites
Thank you my question is answered

Share on other sites
[quote name='Brother Bob' timestamp='1353166388' post='5001769']
You can only call const-functions on a const object
[/quote]
That's not right. You probably meant to say something else...

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 on other sites
[quote name='Álvaro' timestamp='1353170268' post='5001780']
[quote name='Brother Bob' timestamp='1353166388' post='5001769']
You can only call const-functions on a const object
[/quote]
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 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 on other sites
[quote name='Brother Bob' timestamp='1353171150' post='5001787']
You can call anything on a non-const object, but only const-functions on a const object.
[/quote]
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.