C++ - Why static member function can't be created with 'const' qualifier

prabhakaran picture prabhakaran · Aug 12, 2011 · Viewed 50.8k times · Source

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?

Answer

James McNellis picture James McNellis · Aug 12, 2011

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.