多站点执行:通过继承摆脱虚拟表(遗留代码)

发布于 2024-12-03 10:17:56 字数 888 浏览 2 评论 0原文

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

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

有趣的是,不仅有继承,还有模板和怪异的概念...

这是代码

// "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_];
}

如何摆脱继承?

编辑/根据前几个答案,我可以告诉您知道我无法实现更好的序列化,代码是私有的。

I've been stuck for some time on this problem, and I need your help.

My C++ application is running on multiple exec sites. My problem is that I cannot pass objects holding a virtual table, because sites do not share memory (thus a virtual method from a given object will lead to an undefined behaviour). By "I cannot pass" I mean : I do not want any virtual table.

The fun thing is there's not only inheritance, but also templates and eerie conception...

Here is the code

// "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_];
}

How can I get rid of inheritance ?

EDIT/ In light of the first few answers, I can tell you that I cannot implement a better serialization, the code being private.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的痛♀有谁懂 2024-12-10 10:17:56

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

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

If you just want to get rid of virtual tables, you don't have to get rid of inheritance. You have to get rid of virtual functions. Looking at the code you post, maybe you can make a few changes so that getSize and getBuffer are in GenericList, so you can make them non-virtual, but that really depends on the rest of your code.

The first question is, however, why would you worry about virtual tables in the first place? When you serialize the objects, you should serialize their data in order to preserve their state, and the state is the only thing you should pass around.

述情 2024-12-10 10:17:56

我认为你把问题归咎于错误的部分......如果你有一个分布式系统,你必须确保在线发送的序列化数据包含足够的信息来重建另一个对象的状态连接结束。

我相信您面临的问题是您正在通过线路发送原始数据,而您应该有一个序列化机制,能够对正在发送的对象的实际类型进行编码,并使用准确的数据在另一端重建对象相同类型。对于属于具有虚函数的类的对象,这意味着这两个对象按位不相等,因为在连接的每一端,指向 vtable 的指针将引用不同的对象内存中的位置,但它们语义相等,这是您处理另一端对象所需的。

I think you are blaming the wrong part of the problem there... if you have a distributed system, you have to make sure that the serialized data that is sent on the wire contains enough information to rebuild the state of the object on the other end of the connection.

I believe that the problem you are facing is that you are sending raw data over the wire, while you should have a serialization mechanism that is able to encode the actual type of the object being sent and rebuild the object on the opposite end with the exact same type. In the case of an object belonging to a class with virtual functions, that will mean that the two objects are not equal bitwise, as on each end of the connection the pointer to the vtable will refer to a different location in memory, but they will be semantically equal, which is what you need to be able to process the objects on the other end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文