Hi All,

I would like to know how to implement reflection is c++ as this feature it is not supported by c++.

(1)Base *base_ptr= new  derived1();

(2)Base *base_ptr= new  derived2();

(3)Base *base_ptr= new  derived3();

can those derived object ype can be assigned through vector or string?


string list []={"derived1","derived2","derived3"};

so now (1) , (2) and (3) can be replaced by:

(1)Base *base_ptr= new  list[0]();

(2)Base *base_ptr= new  list[1]();

(3)Base *base_ptr= new  listt[3]();

this approach will be very helpful to remove no. of if and else while implementing factory pattern.

Edited by priyanka.choudhary.90038: code formatting

2 Years
Discussion Span
Last Post by rubberman

You cannot do what you want in the matter of what you want. However, if your classes have factory methods, and are registered with a "global" factory, then you can create an instance of these derived classes by calling the factory construction methods with the name. Something like ClassFactory.create("derived1") assuming that "derived1" is a registered class name with the factory. Then ClassFactory can look up the actual class default constructor and make one for you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.