-
call function
hello guys.
i want to func2 could determine that which function has been called it.as i explain in comment in func2 in the below. any help would be appreciated
Code:
#include <iostream>
using namespace std;
void func2(void)
{
//how can i get the function name that call func2
//and also the line that func2 called in that
//and imagine there is more than one file in my project
//then how can i find which file and line and function called
//func2 . the real question is i can not complete func2
// in a way that could figure out what function call this function
}
int func1(int a, int b)
{
if(a>b)
func2();
else
cout << "a+b = " << a+b;
}
void func3(void)
{
func2();
}
-
The obvious approach would be to pass something into func2 - a string perhaps - from the calling function that indicates who called it.
-
If you want a solution that works portably (i.e. with all implementations that comply with the C++ standard) you will need cooperation of the caller - such as passing a string like hk_mp5kpdw suggests.
If you want to do things like debuggers do, then you are reliant on implementation-specific behaviours. In that case, you will need to ask in a forum for programming on your target system, with you compiler of choice.
More generally, however, in what form do you want the "function name"? What do you expect func2() to do with the "function name" it receives?
-
Why would you want to do it anyway?
-
-
What operating system and compiler? What you're probably looking for is called a stack trace. I've done this on Linux with GCC, and the process is (mostly) the same with GCC on any platform. You would use a combination of the backtrace(), backtrace_symbols(), and abi::__cxa_demangle(), along with a few compiler command line options. I don't know what you would do for MS Visual C++, but hopefully, this gets you somewhere. Google can help you further.