我知道如何在静态成员方法中访问静态成员变量-这是我通常使用的两种方法(非常简化):

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的东西来引用静态成员。

最佳答案



没有,没有这样的功能,但是您可以使用本地typedef类:

class MyClass {
    typedef MyClass self;
    static int testValue;
    static int getTestValue1(){
        return self::testValue;
    }
};

看到一个有效的demo

10-08 15:51