C ++ - 为什么静态成员函数不能用'const'限定符创建 [英] C++ - Why static member function can't be created with 'const' qualifier
问题描述
今天我有一个问题。我在需要一个静态成员函数,const不是必须的,但更好。但是,我没有成功我的努力。 c>
当你应用 const
限定符到非静态成员函数,它会影响 this
指针。对于 C
类的const限定成员函数,此
指针的类型为 C const *
,而对于不是const限定的成员函数, this
指针的类型为 C * / code>。
静态成员函数没有 this
指针不是在类的特定实例上调用),因此静态成员函数的const限定没有任何意义。
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?
解决方案 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.
这篇关于C ++ - 为什么静态成员函数不能用'const'限定符创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!