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

前端 未结 5 847
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 06:20

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 an

5条回答
  •  隐瞒了意图╮
    2020-12-04 06:51

    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.

提交回复
热议问题