Figure out how method was called?

This is a discussion on Figure out how method was called? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to figure out whether a method was called internally (from another method in the file) or ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Figure out how method was called?

    Is there a way to figure out whether a method was called internally (from another method in the file) or externally (from another file)? Or alternatively, whether or not a method will return to an internal call or an external one?

    Obviously I could add some fake boolean parameter to my methods and just send true or false depending upon where I call it but if I'm fixed with certain method structures I can't do that.
    Last edited by jcafaro10; 02-06-2009 at 03:20 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why do you want to know? Shouldn't a method always do the same thing, given the same input (unless it is meant to do somewhat random things, or get user input or the like), and not care where it was called from?

    Otherwise, wouldn't that be something the debugger can tell you (if you need it for debugging)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm with Anon.

    If you need to do different things when you are calling from another member function, then you should have two private/protected functions and one public function which calls one form of the private function, and the "from within the class" calls the other private function (which may then call the first private function if it makes sense, after or before it does the "internal call only" part).

    Edit: By making functions private you can ensure that they are not being called from outside of the class itself - so only member or friend functions can use that function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  3. Static templated method problem
    By mikahell in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2006, 08:19 AM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21