[英]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 ) (工作示例位于: http : //ideone.com/VHCSbh )
My question is: is there any more explicit way how to access static member variable than ClassName::staticMemberVar
? 我的问题是:除了ClassName::staticMemberVar
之外,还有什么其他更明确的方法来访问静态成员变量?
Is there something like self::
in C++ ? 在C ++中是否有类似self::
东西?
...simply I am looking for something like this
for referencing static members. ......只是我在寻找类似this
的引用静态成员。
Is there something like
self::
in C++ ? 在C ++中是否有类似self::
东西?
No there is no such feature, but you can use a class local typedef
: 没有,没有这样的功能,但是您可以使用类local typedef
:
class MyClass {
typedef MyClass self;
static int testValue;
static int getTestValue1(){
return self::testValue;
}
};
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 阅读详细信息, 请点击这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.