static member function cannot have a cv-qualifier

Started by
2 comments, last by nlbs 15 years, 9 months ago
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 ??
Advertisement
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.
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!
Regards,FortisVenaliterLead Developer - Unsignedhttp://www.unsigned-game.com
Ok thanks I understood

This topic is closed to new replies.

Advertisement