• Advertisement
Sign in to follow this  

Can a reference which is data member of a class refer to the object of same class

This topic is 3050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Firstly, a reference is not a pointer. If you were using a pointer, this would be possible.

As for why it's impossible: consider what the reference "ptr" would refer to for the very first instance of X constructed...

Share this post


Link to post
Share on other sites
Or in other words:
class x    <-- chicken
{
x &ptr; <-- egg
};

Share this post


Link to post
Share on other sites
Quote:
Original post by pablo24
*** Source Snippet Removed ***

don't see your problem there?
I think this would even work as expected, although it is technically incorrect. The object pointed to by this is not of type x when the reference is initialised, as the constructor has not been called yet. Thus initialising the reference isn't legal here, strictly speaking (although the compiler probably won't notice).
Does it matter, and will it work anyway? Probably... but who can tell for sure.

Though the more interesting question is, why would I want a reference to self in an object in the first place. It doesn't seem to have an useful application.

Share this post


Link to post
Share on other sites
Quote:
Original post by pablo24
*** Source Snippet Removed ***

don't see your problem there?
In that example, the chicken is the egg!

Share this post


Link to post
Share on other sites
As far as I know *this in initilizer list should be OK. The members may not have been constructed at this point, though.

Share this post


Link to post
Share on other sites
Quote:
Original post by samoth
why would I want a reference to self in an object in the first place. It doesn't seem to have an useful application.

Exactly. Just use *this.

Share this post


Link to post
Share on other sites
I've done something like this before - every node has a reference to it's parent so you can recursively search for the root of the tree. A parent value of "*this" is used as a terminator.
class Node
{
Node() : parent(*this) {} //construct new root node
Node( Node& parent ) : parent(parent) {} //construct new node

bool IsRootNode() { return this == &parent; }
Node& parent;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
I've done something like this before - every node has a reference to it's parent so you can recursively search for the root of the tree. A parent value of "*this" is used as a terminator.
*** Source Snippet Removed ***


Can you tell me where this code fragement is useful

can it be used in BST

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
every node has a reference to its parent [...] A parent value of "*this" is used as a terminator.

May I suggest using a pointer instead and NULL as a terminator?

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
I've done something like this before - every node has a reference to it's parent so you can recursively search for the root of the tree. A parent value of "*this" is used as a terminator.
*** Source Snippet Removed ***


This can't compile without an operator= implementation, and const-correctness rules won't let you make a proper copy constructor. Honestly, in general, I don't think it's worth it to try to use references as data members in C++.

Share this post


Link to post
Share on other sites
His question does not say that he wants the reference to point to *this.

Answering the question he actually asked. Yes, of course a class can have a reference to the same type. The problem, as someone pointed out before the thread was derailed, is only in how you initialize it. It's possible to declare it, but it may be impossible to instantiate it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement