使用案例:
class A {
static int s_common;
public:
static int getCommon () const { s_common; };
};
通常这会导致以下错误:
错误:静态成员函数“static int A::getCommon()”不能具有cv限定符
这是因为const
只适用于this
指向的对象,而在static
成员函数中不存在this
。
然而,如果允许这样做,static
成员函数的"const"属性可以很容易地与static
数据成员相关联。
为什么C++没有这个特性?有没有逻辑上的原因?
this
指针参数,但似乎这还不够。 - iammilind