I am trying to call a variable in my class using the this keyword in two ways but I am confused with the 2nd way. The correct way of dereferencing happens to be "(*this).num" however, I was wondering why "*(this).num" is not right as well. The error I get with *(this).num is
request for member 'num' in 'this', which is of pointer type çlass const'*
class::class(int n): num(n)
{
cout << "num= " << num << endl;
cout << "this->num" << this->num << endl;
cout << "(*this).num" << (*this).num << endl;
}
Because if you define
int i = 9;
int *ptr = &i;
cout<<*(ptr)<<endl;
and call *(ptr) it works. But why doesn't it work in my class?
It's simply a matter of operator precedence. The binary dot operator has a higher precedence than the unary star operator, so *(this).num
(the parentheses have no effect there) is interpreted as *(this.num)
, and not as (*this).num
. The compiler is telling you that, because this
is a pointer, this.num
doesn't make sense: you can't apply the dot operator directly to a pointer.
.
has higher precedence than *
.
So writing *(this).num
is equivalent to (*((this).num)))
. Or *(this.num)
.
Your second example is completely different from the first since there is no access to members .
or ->
.
If you don't know all the precedences, or even if you do, it's usually more readable to add the appropriate brackets.
You are using two operators: the indirection/dereferincing operator *
and the member access operator .
.
If you have a look at the precedence of these operators , you'll see that .
has higher precedence than *
(that is, .
will be applied before *
), so thus *(this).num
is basically the same as writing *(this.num)
.
Since this
is a pointer, you can't use the .
operator on it, which is also what the error message is telling you (try using ->
instead).
The reason why your second example works, is that you're not using the .
operator, and thus there is no precedence to be messed up.
One works and the other doesn't because they are not the same thing!
*(ptr)
and *(this)
are the same, but *(this).num
and (*this).num
are not the same, that's the whole point of adding the parentheses! They change how the sub-expressions are grouped, just like in mathematics.
The parentheses in (ptr)
and (this)
are completely redundant, you are grouping a single sub-expression, which does nothing. In (*this)
it's not redundant, it ensures that you dereference the pointer, so in (*this).num
it dereferences the pointer first and then the member access .num
is applied to the result of that dereference.
Compare it to mathematics:
(1) is just 1, and similarly (ptr)
is just ptr
-(1) is just -1, and similarly *(ptr)
is just *ptr
But -(1 + 3) and -(1) + 3 are completely different, because you change the order of the operators.
Similarly, *(this.num)
and (*this).num
are completely different.
The short answer is syntax.
(ptr) looks like this: evaluate expr inside () first, then dereference the result which is in this case is int . It's fine, the same as *ptr.
*(this).num is eq. with *this.num which means get the num member of this. After that dereference num. It's incorrect as you can see because "this" is a pointer to the current object.
(*this).num means dereference this, then get the num member.
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.