|
-
March 21st, 2006, 01:11 PM
#1
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
-
March 21st, 2006, 01:22 PM
#2
Re: Can a member function know who called it?
 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.
-
March 21st, 2006, 03:41 PM
#3
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).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 21st, 2006, 03:59 PM
#4
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
March 21st, 2006, 04:07 PM
#5
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!
-
March 21st, 2006, 04:09 PM
#6
Re: Can a member function know who called it?
 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!
-
March 21st, 2006, 05:14 PM
#7
Re: Can a member function know who called it?
Have a read through this thread.
-
March 22nd, 2006, 04:19 AM
#8
Re: Can a member function know who called it?
 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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 22nd, 2006, 08:01 AM
#9
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|