c++ - 多站点执行 : Getting rid of virtual table with inheritance (legacy code)

标签 c++ arrays pointers inheritance virtual

我在这个问题上被困了一段时间,我需要你的帮助。

我的 C++ 应用程序在多个 exec 站点上运行。我的问题是我不能传递持有虚拟表的对象,因为站点不共享内存(因此来自给定对象的虚拟方法将导致未定义的行为)。 “我不能通过”是指:我不想要任何虚拟表。

有趣的是不仅有继承,还有模板和诡异的构思……

这是代码

// "Main" code
List< Animals, 5 > list;
List< Animals, 8 > list2;
list.concatenate( list2 );

// GenericList.hpp
template< Type >
class GenericList 
{
 virtual getBuffer(void) = 0;
 virtual getSize(void) = 0;
 void concatenate( GenericList<Type> gList)
 {
  int size = gList.getSize(); // Call to the child...
  ...getBuffer()...
  // processing, etc.
 }
}

// List.hpp
template< Type, Size_ >
class List : public GenericList< Type >
{
 int getSize()
  {
   return Size_;
  }
 Type * getBuffer()
  {
   return buffer;
  }
 Type buffer[Size_];
}

我怎样才能摆脱继承?

编辑/根据前几个答案,我可以告诉你我不能实现更好的序列化,代码是私有(private)的。

最佳答案

如果你只是想摆脱虚拟表,你不必摆脱继承。你必须摆脱虚拟功能。查看您发布的代码,也许您可​​以进行一些更改,使 getSizegetBuffer 位于 GenericList 中,这样您就可以将它们设为非- 虚拟的,但这实际上取决于您的其余代码。

然而,第一个问题是,您为什么首先要担心虚拟表?当您序列化对象时,您应该序列化它们的数据以保存它们的状态,而状态是您唯一应该传递的东西。

关于c++ - 多站点执行 : Getting rid of virtual table with inheritance (legacy code),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7346088/

相关文章:

java - 冒泡排序函数不对给定数组进行排序

javascript - 类与原型(prototype) Array.isArray

C 打印出一个字符串数组

c++ - 包装的 unique_ptr 的模板化 move 构造函数

c++ - 存储不同类型的数据c++

c++ - Qt 和 OpenGL : texture transparency

C++简单读取注册表问题

c++ - C/C++ 中的快速图像缩小

c - 指向数组的指针和动态内存分配

c++ - 将带有字符串参数的函数指针从 Swift 传递到 Obj C++