有没有办法像我的代码一样在C++中调用多形反射
is there a way to call polymophism reflection in C++ like my code?
在C++中,我将类Base
作为接口类和 2 个继承类: 从Base
类中Base1
和Base2
如下:
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();
};
以正常方式,在我的 main.cpp 中,我必须包含以下代码:
Base *b;
string base_name = "Base1"; // or "Base2"
if(base_name.compare("Base1") == 0){
b = new Base1();
}else{
b = new Base2();
}
所以,我想使用Base *b = base_name()
而不是上面的if()else()
块。在 C++ 中,这可能吗,嗯?谢谢!
与Java不同,C++不提供对反射概念的原生支持。您可以编写一个简单的函数来构造特定的Base
实例。这称为工厂:
Base* create_base_instance(string name) {
if (name == "Base1")
return new Base1();
if (name == "Base2")
return new Base2();
throw runtime_error("unknown class name");
}
Base *b;
string base_name = "Base1"; // or "Base2"
b = create_base_instance(base_name);
简短的回答是否定的。长答案是:如果你想要一个惯用的解决方案,请查找工厂方法设计模式。
另一种方法:您可以在映射中保留由字符串键入的代理对象,并使用该映射创建这些代理对象(并在需要时克隆它们)。此方法对最终用户隐藏比较,并将其深入到标准库容器中。
使用 std::map 实现工厂模式。
template<typename T>
struct Factory
{
static Base* Create()
{
return new T();
}
};
typedef std::map<std::string, Base* (*)()> FunMap;
FunMap fun;
void Register()
{
fun.insert(std::pair<std::string, Base*(*)()>("Derived1", Factory<Derived1>::Create));
fun.insert(std::pair<std::string, Base*(*)()>("Derived2", Factory<Derived2>::Create));
}
void CreateTypeDemo2(const std::string& name)
{
Base* bp = fun[name]();
bp->Name();
}
从 main 开始,您可以通过这种方式调用函数。
Register();
CreateTypeDemo2("Derived1");
CreateTypeDemo2("Derived2");
您甚至可以如下所示进行操作。在方法 CreateDerived2
中,你可以有不同的创建 Derived2() 的实现,而不是默认的实现
工厂::创造给我们。
Base* CreateDerived2()
{
return new Derived2();
}
void Register()
{
fun.insert(std::pair<std::string, Base*(*)()>("Derived1",Factory<Derived1>::Create));
fun.insert(std::pair<std::string, Base*(*)()>("Derived2",CreateDerived2));
}
希望这有帮助。
相关文章:
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 为什么在C++中对链表这样做?(像堆叠一样处理它们)
- 堆栈和队列是否像C++中的数组一样传递?
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- 访问数据成员(本身是对象)的数据成员,就好像它们是类成员一样
- 我们可以将集合的值存储在变量中吗?就像我们可以将数组的值存储在变量中一样
- 我如何在C++像在 Python 中一样实现 f 字符串?
- 自制的上衣:看起来一样,但不完全相同
- 如何使用 redis-plus-plus 存储二进制数据,就像我想存储结构一样?@for_stack?
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 像 CPP 中的控制台一样的应用程序
- 为什么C++中没有常量引用,就像常量指针一样?
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 有没有像给定的一样的 std::optional_function
- Java 是否像C++模板一样具有泛型推论?
- 记忆栅栏和记忆屏障是一样的吗
- 对象作为参数传递,就好像我们正在传递构造函数值一样
- 像自定义类一样构造的指针(内置类型)如何工作?
- JavaScript箭头函数:我们能否像C ++ lambdas一样捕获值