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?

I.e

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.