rkb
rkb

Reputation: 11231

How do I figure out how many classes are being derived from a particular class at runtime in c++

Lets say I have a Base class as Parent and I want to know how many classes are derived from this class at runtime and create objects of all those classes along with calling the pure virtual function I declared in my Parent class.

How to do this in c++

Upvotes: 0

Views: 127

Answers (1)

anon
anon

Reputation:

I can't see why you would want to do it, and in fact it is not possible under all circumstances - you will need to instantiate all the classes you are counting. One approach is for each derived class will need a static member indicating if it has informed the base class that it is derived - this gets called from the derived class's constructor, but only once per derived class. The base then keeps a static counter of the number of times it has been so informed.

Upvotes: 2

Related Questions