jave.web
jave.web

Reputation: 15052

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.

Upvotes: 1

Views: 955

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

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.

Upvotes: 3

umut
umut

Reputation: 82

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

Upvotes: 0

Related Questions