# Pointing to Itself

## Recommended Posts

Sir Sapo    769
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 on other sites
Roboguy    794
Maybe I'm misunderstanding you, but isn't that what this is?

##### Share on other sites
Sir Sapo    769
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 on other sites
Roboguy    794
Quote:
 Original post by Sir SapoSo 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 on other sites
ApochPiQ    23058
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 on other sites
Sir Sapo    769
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 on other sites
ApochPiQ    23058
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 on other sites
chowe6685    349
But why - what higher level effect are you trying to achieve by doing this?

##### Share on other sites
Sir Sapo    769
OK, I've decided that the way I was planning was not the right course of action, thanks for the help!