【发布时间】:2011-12-27 07:41:13
【问题描述】:
我刚刚注意到我们可以通过成员选择运算符(.或->)访问c++静态成员函数
例如:
class StaticTest
{
private:
int y;
static int x;
public:
StaticTest():y(100){
}
static int count()
{
return x;
}
int GetY(){return y;}
void SetY(){
y = this->count(); //#1 accessing with -> operator
}
};
以下是使用方法
StaticTest test;
printf_s("%d\n", StaticTest::count()); //#2
printf_s("%d\n", test.GetY());
printf_s("%d\n", test.count()); //#3 accessing with . operator
test.SetY();
- #1 和#3 的用例是什么?
- #2 和#3 有什么区别?
在成员函数中访问静态成员函数的另一种风格#1是
void SetY(){
y = count(); //however, I regard it as
} // StaticTest::count()
但现在它看起来更像 this->count()。两种风格调用有什么区别吗?
谢谢
【问题讨论】:
-
this->count()?那是对的吗。我很困惑! -
@ErichLancaster,我在 VC 2010 中测试过,可以构建。
标签: c++ visual-c++