in C++, i have the class Base as a interface class and 2 inheritance class: Base1 and Base2 from Base class like this:
class Base{
public:
virtual void printSomething() = 0;
// Some bla bla code...
};
class Base1 : public Base{
public:
Base1();
void printSomething();
};
class Base2 : public Base{
public:
Base2();
void printSomething();
};
In normal way, in my main.cpp, i have to include following code:
Base *b;
string base_name = "Base1"; // or "Base2"
if(base_name.compare("Base1") == 0){
b = new Base1();
}else{
b = new Base2();
}
So, i want to using Base *b = base_name() instead of if()else() block above. In c++, is this possible, anh how? thanks!
Unlike Java, C++ does not offer native support of the reflection concept. You can write a simple function to construct the specific
Baseinstance. This is called a Factory: