Sign in to follow this  
Padala Srikant

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

Recommended Posts

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this