Sign in to follow this  

const ending

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

Could any one remind me what does such function definition mean? Saw it somewhere and used also, but can't remind....8( //////////////////////////////////////////////////// int somufunc(int par1,float par2, etc...) const; //////////////////////////////////////////////////// THis case is not so clear as ordinar qualificators const, volatile,extern and others...

Share this post


Link to post
Share on other sites
It's only applicable to member functions, it states that the method does not modify the state of the instance (except for mutable data members) its also the only type of member functions that constant instances can only invoke.

All accessor and query methods should be constant member functions regardless.

Share this post


Link to post
Share on other sites

const double foo::wibble(const double bar) const {
^^1^^ ^^2^^ ^^3^^


const #1 claims that the returned value is constant.
const #2 claims that bar is constant.
const #3 claims that *this is constant. (Of course 'this' is constant; you can't just change which object you're working with in mid-stream.)

Share this post


Link to post
Share on other sites
Quote:
const #3 claims that *this is constant.

It changes this from a constant pointer to a constant pointer to constant data.

Share this post


Link to post
Share on other sites
It's also worth noting that unlike return types the trailing const qualifier is part of the function's signature. And thus it's valid to overload a function in both a constant and non-constant form (probably returning constant and non-constant pointers), in which case the compiler prefers the non-constant version.

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
in which case the compiler prefers the non-constant version.


It's dependant on the context it's not a compiler preference

if you invoke a method inside another constant member function then the constant version is invoked.

if you have a constant reference to an instance or a pointer to constant data the constant version is invoked, etc, etc

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Quote:
Original post by doynax
in which case the compiler prefers the non-constant version.


It's dependant on the context it's not a compiler preference

if you invoke a method inside another constant member function then the constant version is invoked.

if you have a constant reference to an instance or a pointer to constant data the constant version is invoked, etc, etc
Eh, wasn't that pretty much what I said?
The compiler will choose the non-constant version if possible, and the constant otherwise. I.e. it prefers the non-constant version.

At least that's what I've always thought.

[Edited by - doynax on May 17, 2005 3:51:50 PM]

Share this post


Link to post
Share on other sites

This topic is 4596 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this