Solved

RTTI Question/Problem - trying to print class name

Posted on 1998-12-08
4
222 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
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
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
Comment Utility
Try

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

Author Comment

by:staggart
Comment Utility
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
Comment Utility
>>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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now