【发布时间】:2015-12-31 14:29:52
【问题描述】:
我试图以两种方式使用 this 关键字在我的类中调用一个变量,但我对第二种方式感到困惑。正确的解引用方法恰好是“(*this).num”,但是,我想知道为什么“*(this).num”也不正确。我得到的错误 *(this).num 是
请求'this'中的成员'num',它是指针类型çlass const'*
class::class(int n): num(n)
{
cout << "num= " << num << endl;
cout << "this->num" << this->num << endl;
cout << "(*this).num" << (*this).num << endl;
}
因为如果你定义了
int i = 9;
int *ptr = &i;
cout<<*(ptr)<<endl;
并调用 *(ptr) 它可以工作。但为什么它在我的课堂上不起作用?
【问题讨论】:
-
您的代码与问题 I. Ed. 不同。括号的位置
-
我的问题是为什么 astrick 需要在类的括号内,而另一方面,在另一个完全不同的情况下,括号外的 astrick 并不重要。我知道这与在我的班级中调用“num”变量有关,但不知道 excat 原因@EdHeal
标签: c++ class pointers this dereference