Can a member function know who called it?
Greetings,
Another ridiculous question. I highly doubt it is possible to do the following but I might as well ask first.
Is there a way for a member function to know which object called it?
Assume the following:
Code:
class A
{
void DoSomething();
}
class B
{
A* pA;
void SetAPointer(A* p){pA = p};
}
class C
{
A* pA;
void SetAPointer(A* p){pA = p};
}
and then in the code we do:
Code:
A my A;
B myB;
myB.SetAPointer(&A);
C myC;
myC.SetAPointer(&A);
myB.pA->DoSomething();
myC.pA->DoSomething();
The question is, inside those two last calls, inside DoSomething() is it possible to know who called it? Perhaps some way of getting the pointer to the object calling it?
Thanks :)
Re: Can a member function know who called it?
Quote:
Originally Posted by greekgoddj
The question is, inside those two last calls, inside DoSomething() is it possible to know who called it? Perhaps some way of getting the pointer to the object calling it?
You want to know in which object was the A* pointer stored, in the DoSomething procedure?
No, it is not possible like that!
A pointer does not keep trace of all copies that were made of it!
And, really, I don't see the point.
Re: Can a member function know who called it?
Not in the situation you've described, no (unless, of course, you pass the object as an argument to the function).
Re: Can a member function know who called it?
You can work your way with a container totally devoted in keeping the trace of function calls - say the last 10 calls made.. that container would be a global one.. accessible from anywhere.. when making calls to functions.. push values into it.. and keep on removing the older ones.. something like a queue.. But what do you need this for? It's a lot of over-head and should not be in production code.. Regards.
Re: Can a member function know who called it?
Alright,it is as I thought. I know I can pass the object calling the function in the function's argument list, but there are a lot of functions that need this capability and was hoping to avoid changing all the functions.
Thanks!
Re: Can a member function know who called it?
Quote:
Originally Posted by exterminator
You can work your way with a container totally devoted in keeping the trace of function calls - say the last 10 calls made.. that container would be a global one.. accessible from anywhere.. when making calls to functions.. push values into it.. and keep on removing the older ones.. something like a queue.. But what do you need this for? It's a lot of over-head and should not be in production code.. Regards.
Ohh I just posted a reply and then saw your post. Yeah I see your suggestion, and also see how it would be quite an overhead. It seems I will have to go ahead with passing a pointer to the function of th calling objects.
thanks people!
Re: Can a member function know who called it?
Have a read through this thread.
Re: Can a member function know who called it?
Quote:
Originally Posted by greekgoddj
Alright,it is as I thought. I know I can pass the object calling the function in the function's argument list, but there are a lot of functions that need this capability and was hoping to avoid changing all the functions.
Thanks!
Which immediately says to me one of two things:
- Observer pattern
- Design flaw.
Which of those two is relevant depends on exactly what you're trying to achieve.
If you can give a few more details of why all these functions need to know who's calling them, then I can expand a bit on the two options above.
Re: Can a member function know who called it?
Easy for a member function to know what object is calling it. Have some kind of unique identifier in each object. Make it a private class member if you don't want this id visible outside of the class.
If all you want is the pointer of the object calling it, then "this" is the pointer.