• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

address of "this" in c++

toughwimp11

Senior member
In c++, I'm have a node class which can make other nodes and when it does, it sets up that node's parent field (which is of type Node *) to the node that is making it. Now, I'm not sure how to pass the address of the object I'm currently working in. I figured, it'd be &this but that gives me an error. Just 'this' works but when i check it, the value is null (0).

The error I get with &this is error: lvalue required as unary ‘&’ operand

Any suggestions.
 
Possibly stupid question (I'm more of a Java guy): You're not trying to do "this" from a static method, are you?
 
The 'this' keyword is already a pointer (ie: A reference) to the current object. Thus, if you use &this you are requesting the memory address of a memory address.

If 'this' evaluates to 0, at least in my mind, there is something else wrong in your code. Can you provide your code?
 
Possibly stupid question (I'm more of a Java guy): You're not trying to do "this" from a static method, are you?

Accessing "this" from a static method results in a compilation error.

I'm in agreement with Gamingphreek that there is a bug in your code. You are trying to call a method on a NULL pointer.

Also, for a class of type C, "this" is already of type C*, and it is not an actual variable so you cannot obtain its address via &.
 
opps, nevermind, i was making a very stupid mistake. in the constructor i never actually set the parent field to the Node * parameter that was passed so parent never got assigned. Once I did that, it works as it should.

thank you guys!
 
Back
Top