Jump to content
  • Advertisement
Sign in to follow this  
Sir Sapo

Pointing to Itself

This topic is 4480 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

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!