Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 10th, 2008, 07:25 AM
Peng Yu
Guest
 
Posts: n/a
Default How to get the class name inside the class's member function?

Hi,

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

Thanks,
Peng
  #2  
Old July 10th, 2008, 07:35 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: How to get the class name inside the class's member function?

"Peng Yu" <PengYu.UT@gmail.comwrote in message
news:a8eebfae-9739-4967-8fbf-06f9d2da31fa@k37g2000hsf.googlegroups.com...
Quote:
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;
}


  #3  
Old July 10th, 2008, 08:35 AM
tony_in_da_uk@yahoo.co.uk
Guest
 
Posts: n/a
Default Re: How to get the class name inside the class's member function?

On Jul 10, 3:25 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
Quote:
"Peng Yu" <PengYu...@gmail.comwrote in message
news:a8eebfae-9739-4967-8fbf-06f9d2da31fa@k37g2000hsf.googlegroups.com...
Quote:
__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.
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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.