Pointing to Itself
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.
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)
*Object getReference()
{
return *this;
}
will that return a pointer to the Object that called it (Object is just an arbitrary class name)
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?
Maybe if you describe what you're trying to solve someone can suggest an alternate approach?
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.
Let's clarify a bit here. Is this what you want?
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]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement