开发者

Multi-site execution : Getting rid of virtual table with inheritance (legacy code)

开发者 https://www.devze.com 2023-04-03 06:24 出处:网络
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 s

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 bein开发者_如何学运维g private.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号