Sign in to follow this  
Sir Sapo

Pointing to Itself

Recommended Posts

Hey everyone, Is there anyway to have an object give a pointer to itself from within that object? I don't really know how else to say it, so let me know if that is really vague and unanswerable.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sir Sapo
So if I have a function:

*Object getReference()
{
return *this;
}

will that return a pointer to the Object that called it (Object is just an arbitrary class name)


Ahh, so you're looking for information about the function which called a function? I don't think that's possible, without passing the information as a parameter.

Share this post


Link to post
Share on other sites
Why would you need such a thing? A function should have no need to return a reference to the thing that called it. The thing that called it always knows itself.

Maybe if you describe what you're trying to solve someone can suggest an alternate approach?

Share this post


Link to post
Share on other sites
Quote:
Ahh, so you're looking for information about the function which called a function? I don't think that's possible, without passing the information as a parameter.


Well, I want an Object to have a function that returns a reference to the Object that called it.

EDIT: Sorry just saw ApochPic's post.

I just want to get a pointer to the object that called a function from within that object.

Share this post


Link to post
Share on other sites
Let's clarify a bit here. Is this what you want?

Object& Object::GetReference()
{
return (something goes here!);
}

void Object::foo()
{
Object thing;
Object& ref = thing.GetReference();

// ref now contains the current object, i.e. *this
}



If that's the correct interpretation, then you can simply use "this" in B::foo() and you're done, you don't have to talk to A at all.

I have a feeling, though, that this isn't at all what you're really after [smile]

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