Is there is any method to find from where a particular function is being called?????????

Dear All,
    I just want to know if there is any in built function available in c++ to find from where the function is being called within a group of files.

Example :


            //To print from where this function is being called
           // The answer should be ---------  class1::f1()

Who is Participating?
macros __FILE__ __FUNCTION__ __LINE__ give the names of current source file, current function and line number ... If you can use them it will be easy enough for you ...

It is possible to retrieve the name of the function from which current function was called but that would need reading the function call stack and getting the name from there ... As you might have guessed, it be highly implementation specific ... I do not think there is an API for this
Someone correct me if I'm wrong, but this isn't possible.  

You can do several things though

1) pass a pointer to the method if you're looking to call it
2) make a parameter where the caller must pass a string - hokey

Maybe you can tell us what you  are trying to do?
I think in DEBUG mode (msvc) you could probably do some calls to inspect the call stack, you might have to go into machine code to inspect it, but if its available to the debugger, its available to you.

Only someone with more than a million points would know how though. c'mon jkr.

Maybe you should post this in the C++ folder
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

why not just add a parameter of "context" to force the caller to identify himself
why not just add a parameter of "context" to force the caller to identify himself
grep and/or find on unix? Search on MS??

In principle, methods are designed to be called from many places (re-usability). Why do you need to know where it was called from??

As a humorous aside, one software shop insisted we document (INSIDE THE FILE THAT CONTAINED THE SOURCE CODE FOR THE METHOD) where it was called from!!!!!! Imagine the ridiculous logic of that decision!!!!! Every time you used a method from another file, you had to edit it's source file and add the fact that you called it.

priyashreeAuthor Commented:
Hi sunnycoder,

  Thank you very much for your info.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.