gpt4 book ai didi

C++ 多态性 + 用于选择返回类型的模板成员函数。怎么做?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:51:59 26 4
gpt4 key购买 nike

我想为对象分派(dispatch)实现一个类层次结构。不同的类调度不同的元素,每个类可以调度其表示为不同数据类型的元素。

通过一个(错误的)例子可以更好地理解。如果允许虚函数模板化,这就是我想要的:

class Dispatcher {
template <class ReturnType>
virtual ReturnType getStuffAs();
};

这样我就可以将子类实现为:

class CakeDispatcher : public Dispatcher {
template <>
virtual Recipe getStuffAs(){ ... }
template <>
virtual Baked getStuffAs(){ ... }
};
class DonutDispatcher : public Dispatcher {
template <>
virtual Frozen getStuffAs(){ ... }
template <>
virtual Baked getStuffAs(){ ... }
}

以便我稍后可以执行以下操作:

void function( Dispatcher * disp ) {
// Works for Donut and Cake, but result will be a different Baked object
Baked b = disp->getStuffAs<Baked>();
// works if disp points to a DonutDispatcher
// fails if it is a CakeDispatcher
// can be compiling/linking time error or runtime error. I don't care
Frozen f = disp->getStuffAs<Frozen>();
}

要求/约束​​:

  • 事先并不知道所有可能的返回类型。这就是我“需要”模板的原因。
  • 每个类只能提供一些返回类型。
  • 类必须有一个共同的祖先,这样我就可以通过指向父类的指针存储对象,并通过这个指针调用函数。
  • 编辑:我不能使用 C++11 功能,但我可以使用 boost 库。

我考虑过但不是解决方案的事情:

  • 显然,虚拟模板函数
  • Curiously Recurring Template Pattern : 打破了共同祖先的条件
  • 使用某种包含子类功能的特征类,但它不起作用,因为父类中的非虚拟实现无法访问此信息

我也许可以在父类中存储一些 typeid 信息,由子类在构造时传递。这使得非虚拟父调度方法可以将自身动态转换为子类型......但它看起来非常丑陋,我不知道这是否会导致某种循环引用问题。

class Dispatcher {
private:
typeid(?) childType;
public:
Dispatcher(typeid childT) : childType(childT) {}

// NOT VIRTUAL
template <class ReturnType>
ReturnType getStuffAs()
{
// or something equivalent to this cast, which I doubt is a correct expression
return dynamic_cast<childType *>(this)->childGetStuffAs<ReturnType>();
}
};

然后子类将实现 childGetStuffAs 函数,这些函数也不是虚拟的。我已经阅读了大约 5-10 个相关问题,但所提供的解决方案似乎都不适合这个问题。

你们能想出更好的解决方案吗?是否有解决此问题的标准模式/技术?

编辑:真正的问题在实际问题中,我有物理模型,其属性可以用多种方式表示:函数、矩阵、概率分布、多项式和其他一些(例如,非线性系统可以表示为函数,但不能表示为矩阵,而线性系统可以转换为两者)。

还有一些算法可以模糊地使用这些模型,但它们可能需要某些模型特征的特定表示。这就是“getStuffAs”函数的原因。整个想法有点复杂——在这里无法正确解释——但我可以保证在这种情况下接口(interface)定义明确:输入、计算和输出。

我的意图是让这成为可能,假设可能的表示形式的数量是事先完全定义的,并且可以将产品转换为无法修改的现有类型/类。然而,我开始意识到这确实不可能以一种简单的方式实现——我不想只为这个问题编写一个库。

最佳答案

#include <cstdio>
// as a type identifier
struct stuff {
virtual void foo() {}
};

template <typename T>
struct stuff_inh : stuff {
};

struct Dispatcher {
template <typename T>
T* getStuffAs() {
return (T*)((getStuffAsImpl( new stuff_inh<T>() )));
}

virtual void* getStuffAsImpl(void*) = 0;
virtual void type() {printf("type::dispatcher\n");}
};

struct Cake : public Dispatcher {
void* getStuffAsImpl(void* p) {
stuff* s = static_cast<stuff*>(p);
printf("cake impl\n");
if (dynamic_cast<stuff_inh<Cake>*>(s) == NULL) {
throw "bad cast";
}
return (void*)(new Cake());
}
virtual void type() {printf("type::Cake\n");}
};

struct Rabbit : public Dispatcher {
void* getStuffAsImpl(void* p) {
stuff* s = static_cast<stuff*>(p);
printf("rabbit impl\n");
if (dynamic_cast<stuff_inh<Rabbit>*>(s) != NULL) {
return (void*)(new Rabbit());
}
else if (dynamic_cast<stuff_inh<Cake>*>(s) != NULL) {
return (void*)(new Cake());
}
else {
throw "bad cast";
}
}
virtual void type() {printf("type::Rabbit\n");}
};

void foo(Dispatcher* d) {
d->getStuffAs<Cake>()->type();
d->getStuffAs<Rabbit>()->type();
}


int main() {
Rabbit* r = new Rabbit;
foo(r);
Cake* c = new Cake;
foo(c);
}

我不确定这个丑陋的解决方案是否正确,希望对您有所帮助。 >_<

没有对资源的删除进行编码以使其看起来更清晰。

关于C++ 多态性 + 用于选择返回类型的模板成员函数。怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26890257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com