Jump to content

  • Log In with Google      Sign In   
  • Create Account


Small question about functions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 bigdilliams   Members   -  Reputation: 118

Like
1Likes
Like

Posted 17 November 2012 - 09:29 AM

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 ........;
}


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7780

Like
3Likes
Like

Posted 17 November 2012 - 09:33 AM

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.

#3 bigdilliams   Members   -  Reputation: 118

Like
0Likes
Like

Posted 17 November 2012 - 09:37 AM

Thank you my question is answered :D

#4 Álvaro   Crossbones+   -  Reputation: 11881

Like
0Likes
Like

Posted 17 November 2012 - 10:37 AM

You can only call const-functions on a const object

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

#5 Álvaro   Crossbones+   -  Reputation: 11881

Like
0Likes
Like

Posted 17 November 2012 - 10:40 AM

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.

#6 Brother Bob   Moderators   -  Reputation: 7780

Like
1Likes
Like

Posted 17 November 2012 - 10:52 AM


You can only call const-functions on a const object

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

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.

#7 Álvaro   Crossbones+   -  Reputation: 11881

Like
0Likes
Like

Posted 17 November 2012 - 11:49 AM

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.

#8 SiCrane   Moderators   -  Reputation: 9387

Like
0Likes
Like

Posted 17 November 2012 - 02:09 PM

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.

#9 Yrjö P.   Crossbones+   -  Reputation: 1412

Like
1Likes
Like

Posted 19 November 2012 - 04:56 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS