Inheritance, order, and packets

Started by
5 comments, last by hplus0603 17 years, 9 months ago
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.
Advertisement
In memory, the Base subobject comes first.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks Fruny, just what I wanted to hear.
note though that if your structure isn't a POD type, its internal structure is undefined.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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).
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement