草庐IT

FooBarAdaptor

全部标签

c++ - 在编译时或运行时将 const char * 映射到 duck-typed T

我有很多类A、B、C、D等,它们都是鸭子类型的,因此具有相同的方法和接口(interface),但不是从同一个类继承的。例如classA{public:voidfoo();voidbar();}classB{public:voidfoo();voidbar();}classC{public:voidfoo();voidbar();}我想在运行时将constchar*映射到这些类之一的相应实例,例如“A”->Aa“B”->Bb这里a是类A的一些实例。或在编译时将'constchar*`映射到相应的类型,例如“A”->A我需要在其他一些函数调用中使用该对象的实例(即调用foo()或bar(