|
-
August 31st, 2009, 07:44 PM
#1
class size changes when inheriting abstract ones...
Hello guys, I'm writing a network library to use in the future on my projects, but I got something that I don't know why is happens...
------------------------------------------------------------------------------------
// IocpServer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
// Struct to store the serialized information
struct SerializedBuffer
{
void* Buffer;
unsigned int Size;
};
class ISerializable
{
public:
virtual unsigned int SerializedSize() = 0;
virtual void Serialize(SerializedBuffer& mBuffer) = 0;
};
/*####################################*/
// This is the packet interface... all packets sent over the network must have this as base
class IPacket
: public ISerializable
{
public:
virtual unsigned int GetProtocolType() = 0;
virtual unsigned int GetProtocolAction() = 0;
virtual unsigned int GetPacketSize() = 0;
virtual unsigned int SerializedSize() = 0;
virtual void Serialize(SerializedBuffer& mBuffer) = 0;
};
/*####################################*/
#pragma pack(push, 1)
class DefaultPacket
: public IPacket
{
private:
unsigned short m_wSize; // Returned by the GetPacketSize(); on its implementation
unsigned short m_wActionType; // Returned by the GetProtocolType(); on its implementation
unsigned short m_wActionIndex; // Returned by the GetProtocolAction(); on its implementation
public:
DefaultPacket(unsigned short wActionType = 0, unsigned short wAction = 0)
{
this->m_wSize = 0;
this->m_wActionType = wActionType;
this->m_wActionIndex = wAction;
}
~DefaultPacket(void)
{
}
unsigned int GetProtocolType()
{
return this->m_wActionType;
}
unsigned int GetProtocolAction()
{
return this->m_wActionIndex;
}
unsigned int GetPacketSize()
{
return this->m_wSize;
}
virtual unsigned int SerializedSize() = 0; // from ISerializable - must be implemented on the final derived class
virtual void Serialize(SerializedBuffer& mBuffer) = 0; // from ISerializable - must be implemented on the final derived class
};
/*####################################*/
class MyAction1
: public DefaultPacket
{
private:
unsigned int MyDataAsNumber;
public:
MyAction1()
: DefaultPacket(0, 1) // Action 0, SubAction 1
{
}
~MyAction1()
{
}
unsigned int SerializedSize()
{
this->m_wSize = sizeof(*this);
return sizeof(*this);
}
void Serialize(SerializedBuffer& mBuffer)
{
mBuffer.Size = this->SerializedSize();
mBuffer.Buffer = this;
}
};
#pragma pack(pop, 1)
int _tmain(int argc, _TCHAR* argv[])
{
// why it displays 10 since I'm using pack() and 3 short vars? why not 6?
printf("sizeof(DefaultPacket) : %d\n", sizeof(DefaultPacket));
// 10 + dword = 14... but i was expecting 10, 4 bytes appeared from nowhere
printf("sizeof(MyAction1) : %d\n", sizeof(MyAction1));
system("pause");
return 0;
}
------------------------------------------------------------------------------------
OBS: Look at the code comments
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|