sizeof(struct) vs. sizeof(class)

Started by
20 comments, last by mkaltner 22 years, 3 months ago
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
"The important thing to remember when programming is: When you're 90% complete, there's still 50% more to go."
Advertisement
The extra 4 bytes is a pointer to the vtable. This will only be there if your class has virtual functions.
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
"The important thing to remember when programming is: When you're 90% complete, there's still 50% more to go."
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.
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).
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
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 .
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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.
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
- 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
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.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement