Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a static member function have a cv-qualifier?

This is the error:

error: static member function ‘static void myClass::myfunct()’ cannot have cv-qualifier

Can someone please explain this error and why const cannot be used.

#include<iostream>
class myClass{      
   static void myfunct() const 
   { 
     //do something
   }
};

int main()
{
   //some code
   return 0;
}
like image 511
Sahil Sareen Avatar asked Nov 06 '13 13:11

Sahil Sareen


People also ask

What limitations does a static member function have?

What limitation does a static member function have? Static member functions can only access member variables that are also static.

Why we can not declare static member function constant or virtual?

A 'const member function' is not allowed to modify the object it is called on, but static member functions are not called on any object. It is used directly by scope resolution operator. Thus having a const static member function makes no sense, hence it is illegal.

What are the restrictions on the use of static member functions?

You cannot have static and nonstatic member functions with the same names and the same number and type of arguments. Like static data members, you may access a static member function f() of a class A without using an object of class A .

Which is not applicable for static member function?

Which among the following is not applicable for the static member functions? Explanation: Since the static members are not property of objects, they doesn't have this pointer. Every time the same member is referred from all the objects, hence use of this pointer is of no use.


5 Answers

Worth quoting the standard here

9.4.1 Static member functions

2) [ Note: A static member function does not have a this pointer (9.3.2). —end note ] A static member function shall not be virtual. There shall not be a static and a non-static member function with the same name and the same parameter types (13.1).

A static member function shall not be declared const, volatile, or const volatile.

static functions have no this parameter. They need no cv-qualifiers.

See this answer by James McNellis

When you apply the const qualifier to a nonstatic member function, it affects the this pointer. For a const-qualified member function of class C, the this pointer is of type C const*, whereas for a member function that is not const-qualified, the this pointer is of type C*.

like image 158
Sadique Avatar answered Oct 24 '22 07:10

Sadique


A static member function is not bound to an instance of its class, so it does not make sense for it to be const and/or volatile (i.e. "cv-qualified"), because there is no instance to which const or volatile can be applied to in calling that function.

like image 31
juanchopanza Avatar answered Oct 24 '22 07:10

juanchopanza


It doesn't make sense to write const there, because the function is static and therefore there is no class instance on which to imbue a const context. Thus it is treated as an error.

like image 22
Lightness Races in Orbit Avatar answered Oct 24 '22 07:10

Lightness Races in Orbit


Qualifier const in a member function declaration is applied to the pointer to the object of class this. As static functions are not bound to objects of the class they have no implicit parameter this. So the qualifier const has no any sense for these functions.

like image 37
Vlad from Moscow Avatar answered Oct 24 '22 07:10

Vlad from Moscow


Const qualifier for member functions means that the function will not change the object instance and can be called on const objects. Static member functions are not bound to any object instance and therefore it makes no sense for them to be const, since you don't call the static member function on any object. That is why the standard forbids it.

class Foo
{
public:
    void memberFunc();
    static void staticMemberFunc();
}

Foo f;
f.memberFunc();          // called on an object instance
Foo::staticMemberFunc(); // not called on an object instance
like image 27
rozina Avatar answered Oct 24 '22 07:10

rozina