Pointing to Itself

Started by
7 comments, last by Sir Sapo 18 years, 1 month ago
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.
My Current Project Angels 22 (4E5)
Advertisement
Maybe I'm misunderstanding you, but isn't that what this is?
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)
My Current Project Angels 22 (4E5)
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.
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?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.
My Current Project Angels 22 (4E5)
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]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

But why - what higher level effect are you trying to achieve by doing this?

OK, I've decided that the way I was planning was not the right course of action, thanks for the help!
My Current Project Angels 22 (4E5)

This topic is closed to new replies.

Advertisement