How to get the class name inside the class's member function?

P

Peng Yu

Hi,

__FUNCTION__ can give the function name. Is there any variable for
class name?

Thanks,
Peng
 
J

Jim Langston

Peng Yu said:
Hi,

__FUNCTION__ can give the function name. Is there any variable for
class name?

If you are lucky, typeid( *this ).name()
Lucky meaning it may do so on some implementations, others it may not.
..name() is not guaranteed to have any value afaik.

On my system, and compiler (VC++ Express 2008) the following program
outputs:
class Foo

Your mileage may vary.

#include <iostream>

class Foo
{
public:
Foo()
{
std::cout << typeid( *this ).name() << "\n";
}
};

int main()
{
Foo Bar;
}
 
T

tony_in_da_uk

If you are lucky, typeid( *this ).name()
Lucky meaning it may do so on some implementations, others it may not.
.name() is not guaranteed to have any value afaik.

Similarly, if you're lucky enough to get to use GCC,
__PRETTY_FUNCTION__ should contain the scoping namespaces/classnames
(and return and argument types). So, some parsing required....

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top