CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    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

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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.

  5. #5
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    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!

  6. #6
    Join Date
    Dec 2004
    Location
    Leamington Spa, UK
    Posts
    202

    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!

  7. #7
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Can a member function know who called it?

    Have a read through this thread.

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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:
    1. Observer pattern
    2. 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


  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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
  •  





Click Here to Expand Forum to Full Width

Featured