Inheritance, order, and packets
This should be a simple question.
So at the moment I have my packet structure set up something like this for my game:
struct stNetHeader
{
...
};
struct stNetGameMessage : public stNetHeader
{
...
};
My question is, when I create a stNetGameMessage, and copy it into a buffer for sending, or when I recieve a buffer to be coppied into an empty message, does the base class(/struct) get copied in first, then the derived, or vise versa?
Thanks.
Quote:Original post by Fruny
note though that if your structure isn't a POD type, its internal structure is undefined.
"In addition, a POD type can't have constructors, virtual functions, base classes, or an overloaded assignment operator."
Wouldn't that mean that my message's internal structure is undefined, since it has a base class?
Thanks for the heads up...time to remove my c-tors and assignment operators.
[Edited by - Driv3MeFar on July 5, 2006 8:40:03 PM]
Yes, you can do something like this though:
struct MessageHeader
{
//...
};
struct MessageX
{
MessageHeader header;
};
In practice virtual functions and/or virtual inheritence are usually the only things that prevent a compiler from using POD layout.
You also need to pack the struture if you are going to put it on the wire:
#pragma pack(push, 1)
struct Message
{
//...
};
#pragma pack(pop)
struct MessageHeader
{
//...
};
struct MessageX
{
MessageHeader header;
};
In practice virtual functions and/or virtual inheritence are usually the only things that prevent a compiler from using POD layout.
You also need to pack the struture if you are going to put it on the wire:
#pragma pack(push, 1)
struct Message
{
//...
};
#pragma pack(pop)
In reality, as long as you don't have a virtual function, and don't have more than one class, the layout will be "base class -> derived class". Your initial suggestion will work on any compilers/platforms you're likely to encounter (including GCC for x86, x86-64 and PPC, and MSVC for x86 and x86-64).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement