我知道如何在静态成员方法中访问静态成员变量——这是我通常使用的两种方式(非常简单):
class S{
private:
static const int testValue = 5;
public:
static int getTestValue0(){
return testValue;
}
static int getTestValue1(){
return S::testValue;
}
};
(工作示例:http://ideone.com/VHCSbh)
我的问题是:有没有比 ClassName::staticMemberVar
更明确的访问静态成员变量的方法?
C++ 中是否有类似self::
的东西?
...只是我正在寻找类似this
的东西来引用静态成员。
最佳答案
Is there something like
self::
in C++ ?
不,没有这样的功能,但你可以使用本地类typedef
:
class MyClass {
typedef MyClass self;
static int testValue;
static int getTestValue1(){
return self::testValue;
}
};
查看工作 demo .
关于c++ - 在静态成员方法中显式访问静态成员变量 - 在 C++ 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32286240/