Sign in to follow this  
nlbs

static member function cannot have a cv-qualifier

Recommended Posts

It Compiles
static const long& getLifetime() {return lifeTime;}

But it doesn't
static const long& getLifetime() const{return lifeTime;}

Error Shown
error: static member function ‘static const long int& SessionConfig::getLifetime()’ cannot have cv-qualifier
is const (that one on the right hand side) a cv-qualifier ?? what does the term cv-qualifier mean ?? and why it cannot have a cv-qualifier ??

Share this post


Link to post
Share on other sites
cv-qualifier is compiler speak for something marking a function as const (in this case the keyword "const" placed after the method declaration.).

A static member function cannot be marked as const because doing so wouldn't make any sense in the first place. const is used in order to mark which functions can and cannot mutate an instance of the class, such that a const object can't be changed by mutator methods but still allows calls to methods marked as const. If there was no const qualifier on these methods, then the compiler wouldn't allow one to call them on a const instance of the object. Since static methods aren't related to any particular instance of the class and can only access static member variables, there is no reason for a static method to be marked as const and therefore the compiler considers this an error.

Share this post


Link to post
Share on other sites
cv-qualifier is basically a generalized term that refers to the keywords const and volatile.

Visual Studio has a number of "precautions" to prevent users from casting something that is cv-qualified to a void* and back to a regular pointer (thus eliminating the cv-qualification). This seems to be one such barrier.

For more info, check out this blog:
http://learningcppisfun.blogspot.com/2005/07/casting-cv-qualifier-and-function.html

Edit: gah! ninja'd!

Share this post


Link to post
Share on other sites

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