Solved

RTTI Question/Problem - trying to print class name

Posted on 1998-12-08
4
239 Views
Last Modified: 2011-09-20
On MSC VC++ 5.0 (SP3), I am trying to print out the name of a
class from a class member function.  In the first case
below, the compiler fatal errors.  In the second case, I get
an obscure error.  Any ideas on how to print a class' name
from withing the class like I am doing?  Thanks.

-------------------------------------------------
#include <iostream>
using namespace std;

#include <typeinfo>

//
// CBorder -- border of an object
//
class CBorder
{
public:
      void      PrintName() {  cout << typeid(this).name << endl; };

};

Here's the error:

junk.cpp(13) : fatal error C1001: INTERNAL COMPILER ERROR
  (compiler file 'msc1.cpp', line 1188)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information

-------------------------------------------------
#include <iostream.h>
// using namespace std;

#include <typeinfo>

//
// CBorder -- border of an object
//
class CBorder
{
public:
      void      PrintName() {  cout << typeid(this).name << endl; };

};


Here's the error:

junk.cpp(13) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'overloaded function type' (or there is no acceptable conversion)
0
Comment
Question by:staggart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1179630
several problems.  name is a member function, not a data member.  also you want to get type info on what this points to, not this.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1179631
Try

void PrintName() {  cout << typeid(*this).name() << endl; };
0
 

Author Comment

by:staggart
ID: 1179632
Yes, you are so right.  My mistake.  I think the compiler should not get a fatal error!

both typeid(this).name() and typeid(*this).name() work.

Thanks.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1179633
>>I think the compiler should not get a fatal error!
Agreed, but at least this one was easy to fix.  Sometimes it is a real pain.  I've been helping someone else (actually, I haven't) on a fatal compiler error for several days now.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question