Discussion:
difference between struct and class
(too old to reply)
thomas
2009-11-13 16:46:54 UTC
Permalink
struct A{
int a;
char b;
};

class B{
int a;
char b;
};

sizeof(A)=6; sizeof(B)=8. why?
Igor Tandetnik
2009-11-13 17:04:02 UTC
Permalink
Post by thomas
struct A{
int a;
char b;
};
class B{
int a;
char b;
};
sizeof(A)=6; sizeof(B)=8. why?
This program prints "8 8" with my copy of VC8:

#include "stdio.h"

struct A{
int a;
char b;
};

class B{
int a;
char b;
};

int main() {
printf("%d %d\n", sizeof(A), sizeof(B));
return 0;
}

I could get it to print "6 6" with #pragma pack or by using /Zp2 compiler switch. The only way I could get it to print different numbers is by putting #pragma pack between two class declarations - but that's cheating.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Scot Brennecke
2009-11-14 08:36:02 UTC
Permalink
Post by thomas
struct A{
int a;
char b;
};
class B{
int a;
char b;
};
sizeof(A)=6; sizeof(B)=8. why?
Is this a brain teaser, or a real issue? Which version of VC++? Is
that the real code, or a simplification of the real code?

Loading...