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;
}
What limitation does a static member function have? Static member functions can only access member variables that are also static.
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.
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 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.
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 bevirtual
. There shall not be astatic
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
, orconst 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 thethis
pointer. For a const-qualified member function of classC
, thethis
pointer is of typeC const*
, whereas for a member function that is not const-qualified, thethis
pointer is of typeC*
.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With