-2

In C++, Is there any way/ procedure to know whether particular class has been inherited by other class?

Is it possible in C++

hims
  • 315
  • 3
  • 10

1 Answers1

3

For example lets say you have the following two classes:

class A {};

class B : public A {};

If you ask if there is a way to know if A have been inherited then no there is no such way.

If you ask if there is a way to know if B inherits from another class (no matter what class it is) then no there is not.

The closest you can get is probably std::is_polymorphic, which tells (compile time) if a class have virtual functions that can be inherited, or if a class have virtual functions that it have inherited. There is no functionality, compile time or run time, to tell if a class have been inherited, or what the base-classes of some class is.

Shoe
  • 70,092
  • 30
  • 150
  • 251
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • I have checked for std::is_polymorphic, is a useful way to know whether class has virtual function that can be inherited. however how would i come to know how many derived classes has inherit the same base class? I think you have covered this in above mentioned replies that there is no such way to know that. Am i correct? – hims Dec 30 '13 at 08:59
  • 1
    @hims Yes, there is no way to know that. For that you need [introspection](http://en.wikipedia.org/wiki/Type_introspection) which C++ doesn't have. – Some programmer dude Dec 30 '13 at 11:10
  • Thanks Joachim for your reply and link aswell. – hims Jan 13 '14 at 10:33