dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Intermediate
Jul 2, 1999

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Class Types Used in Typeid Expressions Must Be Completely Defined

When you use operator typeid, the class type of its argument must be completely defined. This means that a forward declaration of the argument's base class is insufficient, as in this example:

 
  class Base; //forward declaration of a base

  void func(Base *pb)
  {
    if (typeid(*pb) != typeid(Derived))  //trouble
      do_something();
  }

In order to make this code work properly, the declarations of both Derived and Base have to be accessible from the scope of the typeid expression. You can make them accessible by #including their header file:

 
  #include "Base.h" 
  #include "Derived.h" 
   
  void func(Base *pb)
  {
    if (typeid(*pb) != typeid(Derived)) //now fine
      do_something();
  }
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date