Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function name

Status
Not open for further replies.

idinkin

Programmer
Sep 12, 2002
29
0
0
IL
Is there a way to get the name of the function where the program is running now?
 
you can not. Function name makes sence just before compilling, but after compilling, you have no function nor variable names. Only things you have are addresses in the virtual or phisical memory space.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Well if you're using vs.net, then its pretty easy.

If you're just after some basic debug information to help find out where you are, then the FILE and LINE macros are standard.

Code:
#include <stdio.h>
void foo ( ) {
#if _MSC_VER >= 1300
    printf( &quot;Hello from function %s\n&quot;, __FUNCTION__ );
#else
    printf( &quot;Hello from file %s at line %d\n&quot;, __FILE__, __LINE__ );
#endif
}

int main ( ) {
    foo();
    return 0;
}

--
 
Ahem, well, with that approach its quite easy in C++ as well

Code:
cout << &quot;Hello from function foo()&quot; << endl;

But even I realize that'd be a bit silly.

You could use some aspect oriented pre-compile tools to inject &quot;here I am&quot; notifications in all functions, but since aspect oriented programming is a direct violation of the object oriented programming's idea of encapsulation (it rudely ignores any privacy and makes it all accessible - brrrr) I would not really recommend it.

But then - it is your choice to do what you wish with your code.

/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
This may not be exactly what you're asking for, but you can always step through your program in a debugger...
 
function names also makes sence while using dynamic linked libraries. There you can get an address of a function from its name, but not viceversa.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top