1

Say if I have a class named cube which refers to a 3D cube, and it has a private member data called _height. Within the class I tried to use

this->_height

and it works. (I know '_height' along is enough. I just want to try more about 'this' pointer). However when I use

*this._height

inside the class. It reports error. 'this' is a pointer points to the object in use thus 'pointer->member' method is valid. Meanwhile '*this' should be the object itself, but why it forbids me to use the '.' (dot) method?

Xiao ZHANG
  • 35
  • 5

1 Answers1

2

Due to the precedence of the . over the * you need to put brackets around to make sure you get what you want:

(*this)._height
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • If I am reading the edit history correctly, then one second after you accepted an edit proposal, I overwrote it with my own edit that I had been making at the same time. However, I am not sure if this information is correct, because I am also listed as voting on the edit proposal and selecting "Reject and Edit", which implies that my edit did not take place after the edit proposal was applied. As far as I know (I may be wrong), edits are applied immediately when the OP accepts them, so it is unclear to me what exactly happened, and in which order it happened, so I don't know how to fix this. – Andreas Wenzel Oct 02 '22 at 09:29
  • The edit proposal, which is listed as "accepted" but which I overwrote with my edit, added `(unipolar)` to the answer. I am unfamiliar with this word in this context, but I guess it is supposed to mean "unary" as in "unary operator". Since I am unsure of whether you accepted the edit proposal or not, I do not know whether you want it restored or not. – Andreas Wenzel Oct 02 '22 at 09:32
  • Sorry, @AndreasWenzel , I meant to write _unary_. – markoj Oct 02 '22 at 11:38
  • Because `*` can also be used as a binary operator. – markoj Oct 02 '22 at 11:47
  • Although, it's completely unnecessary here, because both operators written with a `*` are of lower precedence than `.`. – markoj Oct 02 '22 at 12:34