Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mkaltner

sizeof(struct) vs. sizeof(class)

This topic is 6095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quick question. I''m implementing a file loading architecture and ran into something strange. Question: Is there an overhead when using classes? When crating the same thing in structure and class, I get a 4 byte difference. Is this correct or did I miss something? Thanks, - Mike

Share this post


Link to post
Share on other sites
Advertisement
The extra 4 bytes is a pointer to the vtable. This will only be there if your class has virtual functions.

Share this post


Link to post
Share on other sites
quote:
Original post by Qoy
The extra 4 bytes is a pointer to the vtable. This will only be there if your class has virtual functions.


Hmm, you learn something every day.
Darn virtual deconstructor. =)

THANKS!

- Mike

Share this post


Link to post
Share on other sites
Something to consider if you are storing structs to a file:

make sure you explicitly specify the structure packing in the header files which declare the struct

in VC++

#pragma pack(2) or something like that

struct
{
...
}


otherwise in the future if you change structure packing size or someone else tries to use your files they may not be able to properly read the structure from the file because their compiler setings may be packing the structure differently which will cause the data members to be read incorrectly.

Share this post


Link to post
Share on other sites
quote:
Original post by mkaltner
Darn virtual deconstructor. =)



2 things to note:
1) It''s destructor, not deconstructor. To deconstruct means to analyze. To destruct means to destroy.
2) Structs can have virtual functions too. The only difference between a class and a struct is the default access (private for class, public for struct).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Stoffel: You''re only partially correct. A struct in C++ is handled as a class with Defualt Public members... a struct in C is a struct that cannot contain member functions, but only data members. Just because MSVC handles structs as classes, doesn''t make it true for everything ;o).

Billy

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Stoffel: You''re only partially correct. A struct in C++ is handled as a class with Defualt Public members... a struct in C is a struct that cannot contain member functions, but only data members. Just because MSVC handles structs as classes, doesn''t make it true for everything ;o).

Billy


Given that C doesn''t have a class keyword, you''ve failed to make any kind of a point.

C++ has both struct and class, and the only difference between the two is the default member access -- private for class, public for struct. That''s it .

Share this post


Link to post
Share on other sites
I was going to reply, but DrPizza did it for me. =)

There is one other slight difference, and I''m not really sure why it is (probably because of the default access), but you cannot inherit classes from structs and vice versa, nor can you forward-declare a class which is actually a struct nor vice-versa. I think this just annoys me more than anything (especially the latter).

So the only difference between structs and classes are the default access _and_ the fact that they are different types.

Share this post


Link to post
Share on other sites
quote:
Original post by Stoffel
...but you cannot inherit classes from structs and vice versa...

I swear I've done this... I know I've had structs that inherited from other structs - and actually I know I've inherited struct from classes. I may not have ever tried to inherit a class from a struct though.

    
struct Packet
{
DWORD PacketID;
};
struct ChatPacket : public Packet
{
WORD wLength;
char* szChatter;
};

struct IMyInterface
{
virtual void DoSomething()=0;
};

class CMyObject : public IMyInterface
{
public:
virtual void DoSomething() {}

};

//don't think I've tried this:

class Stuff
{
int morestuff;
};
struct Zoid : public Stuff
{
};

class SeemsToWork : Zoid
{
int yetmorecrap;
};

Is that what you meant, or something else?

I think structs and class may default to different types of inheritence as well (if you don't specify the type), though I could be mistaken (that's something I just never do).

...
I just tested inheriting a struct from a class and it works fine on MSVC - and structs default to public inheritence whereas classes default to private.

So struct and classes are different types (never though of it like that before, but it seems true enough), default to different member declaration privacy, and default to different inheritence privacy. (What's the real word for privacy here? scope? access class?)

Edited by - Magmai Kai Holmlor on January 9, 2002 11:55:24 PM

Share this post


Link to post
Share on other sites
quote:
There is one other slight difference, and I''m not really sure why it is (probably because of the default access), but you cannot inherit classes from structs and vice versa, nor can you forward-declare a class which is actually a struct nor vice-versa. I think this just annoys me more than anything (especially the latter).

The former, no. MSVC++ 7 disagrees, Comeau disagrees, g++ disagrees.

This code:
  
class CB
{
};

struct CD : public CB
{
};

struct SB
{
};

class SD : public SB
{
};

int main(int argc, char* argv[])
{
CB cb;
CD cd;
SB sb;
SD sd;
}

compiles cleanly (except for unused variable warnings).

The latter is just as well. It would be untidy to see a struct forward declaration and then have it implemented as a class.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!