简体   繁体   中英

Explicitly access static member variable in static member method - in C++

I know how to access static member variable in static member method - these are two ways I usually use (very simplified):

class S{
    private:
        static const int testValue = 5;
    public:
        static int getTestValue0(){
            return testValue;
        }
        static int getTestValue1(){
            return S::testValue;
        }
};

( working example on : http://ideone.com/VHCSbh )

My question is: is there any more explicit way how to access static member variable than ClassName::staticMemberVar ?

Is there something like self:: in C++ ?

...simply I am looking for something like this for referencing static members.

Is there something like self:: in C++ ?

No there is no such feature, but you can use a class local typedef :

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

See a working demo .

There is no support to use something other than class name. You'll need to implement it.

Static Function Members: By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.

to read details click here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM