Today I got a problem. I am in the need of a static
member function, const
is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how?
A static member function cannot be declared with the keywords virtual , const , volatile , or const volatile . A static member function can access only the names of static members, enumerators, and nested types of the class in which it is declared.
The const qualifier at the end of a member function declaration indicates that the function can be called on objects which are themselves const. const member functions promise not to change the state of any non-mutable data members.
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 .
What limitation does a static member function have? Static member functions can only access member variables that are also static.
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*
.
A static member function does not have a this
pointer (such a function is not called on a particular instance of a class), so const qualification of a static member function doesn't make any sense.
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