2

我有以下结构:

class Base {
   virtual T foo() = 0;
};

class Derived : public Base {
   T foo() override { /**/ }
}

class Derived1 : public Base {
   T foo() override { /**/ }
}

我需要以下工作(或适当的替代品):

some_container<unique_ptr<Base>> objects;

基本上,
C++ AMP 不允许在内核中使用虚函数,但我绝对需要一个类似继承链的容器行为。

将这种继承链转换为模板魔术的推荐/常见模式是什么?

4

2 回答 2

1

删除 vtable 调用的规范方法是将其替换为 switch 语句:

enum type_e
{
   type_derived,
   type_derived1
};

class Base
{
public:
   Base( type_e type ) : m_type( type ) {}

   T foo();

private:
   type_e m_type;
};


T Base::Foo()
{
   switch( m_type )
   {
   case type_derived:
      return //{...} your Derived::Foo()
   case type_derived1:
      return //{...} your Derived1::Foo()
   }
}

API 中唯一的变化是new Derived()您必须调用而不是调用new Base( type_derived )。主要缺点是您现在必须将所有附加数据(以前是 Derived1 或 Derived 的成员)保存在 中Base,这可能会使类膨胀。另一方面,您现在可以按值制作 Base 的容器,并避免std::unique_ptr.

于 2013-10-25T23:42:13.587 回答
0

您可以推出自己的手动 vtable 仿真:

class Base {
protected:
    using fp_t = int(*)(Base*);
    fp_t fp;
    Base( fp_t p ) : fp( p ) {}     
public:
    int foo() { return (*fp)(this); }
};

class Derived : public Base {
    static int sfoo(Base* b) { return static_cast<Derived*>(b)->foo(); }
    int foo() { return 42; }
public:
    Derived() : Base(&sfoo) {}
};

活生生的例子

于 2013-10-25T23:41:02.530 回答