10

Suppose I have:

struct Foo: public Bar {
 ....
}

Foo introduces no new member varaibles. Foo only introduces a bunch of member functions & static functions. Does any part of the C++ standard now guarantee me that:

sizeof(Foo) == sizeof(Bar)

?

Thanks!

anon
  • 41,035
  • 53
  • 197
  • 293
  • Also notice that the `Bar` that is contained in `Foo` can have a different size than the `Bar` not contained in a `Foo`. For instance, a common optimization is to make base classes have size zero if they are empty, but nonzero if they are not contained as base-classes (as required by C++). – Johannes Schaub - litb May 07 '10 at 07:09

3 Answers3

11

I could think of at least one scenario where sizeof(Foo) != sizeof(Bar):

class Bar {
public:
 int m_member;
};

class Foo : Bar {
    virtual ~Foo();
};

Foo will have a vtable pointer whilst Bar wouldn't and the former's size will be one word bigger that Bar. On 32bit DEBUG MSVC2010:

sizeof(Foo) - 8
sizeof(Bar) - 4

EDIT This holds true for structs as well as classes, I reran the test to confirm that.

Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
  • @Betamoo: I'd like to hear about the differences and how they relate to this answer. – tiftik May 07 '10 at 01:10
  • 5
    In C++, the only difference between struct's and class's are the default access scope. - with one addition, even though it doesn't really matter, the language asks that you refer to the same construct consistently. You cannot declare "class Foo", then "struct Foo". At least, if you do, it will assume they're the same and (typically) warn about inconsistent declarations. – Nathan Ernst May 07 '10 at 01:12
  • 3
    @Betamoo: C++ has no "structs". Everything is a class, even if it is declared with `struct` keyword. – AnT stands with Russia May 07 '10 at 01:13
  • @Nathan Ernst: Well, there's another difference, see this: http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c/999810#999810 – tiftik May 07 '10 at 01:15
  • @Betamoo `Bar` does not have virtual methods, hence it has no virtual table. `Foo` is the first class that introduces a virtual function - hence it and all of its descendants will have a vtable pointer. – Igor Zevaka May 07 '10 at 01:15
  • @tifik, I wouldn't say that's a difference. struct's are by default public, aren't they?, whether you're declaring one or inheriting one. ;) My statement still holds true. – Nathan Ernst May 07 '10 at 01:19
  • @Igor Zevaka, I'd also argue, that while this is likely the common case, it is *not* guaranteed by standards, as there is not guarantee as to how virtual methods of any sort are implemented or represented. – Nathan Ernst May 07 '10 at 01:22
  • @Nathan Ernst: From what I understood, it's not about member access control but public/private inheritance. – tiftik May 07 '10 at 01:27
  • C++ holds no distinction between a struct and a class, save that structs default to public, while classes default to private members. – Arafangion May 07 '10 at 01:43
  • @Arafangion: Classes declared with `struct` key default to public *subobjects*. The term *subobjects* covers both member subobjects and base class subobjects. – AnT stands with Russia May 07 '10 at 10:30
5

Yes - if they are POD types. POD types are guarenteed to be layout compatable (that is you can memcpy from one to the other) if they have layout-compatable members in the same order. Since a subclass automatically has all of its base class's members in the same order and, in this case, no others, they will be layout compatible and thus the same size. See section 9.3 of the spec.

Note that in order to be POD types they must have no virtual functions (among other requirements)

EDIT

The latest draft standard has split the requirements for POD types into two sets: trivial classes and standard layout classes. POD classes are those that are both trivial and standard layout, and I believe for the sizeof guarentee you want, just being standard layout suffices -- they need not also be trivial (and thus POD) classes. The requirements for standard layout from the spec are:

A standard-layout class is a class that:

— has no non-static data members of type non-standard-layout class (or array of such types) or reference,

— has no virtual functions (10.3) and no virtual base classes (10.1),

— has the same access control (Clause 11) for all non-static data members,

— has no non-standard-layout base classes,

— either has no non-static data members in the most-derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and

— has no base classes of the same type as the first non-static data member.108

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • I don't think the guarantee holds if there are member functions, does it? Certainly the example of the base class with no virtual members and the subclass with virtuals would suggest it doesn't. – Mark Ransom May 07 '10 at 01:38
  • 1
    POD types can have member functions. They can't have virtual member functions though. I'm not convinced that a type with a base type can be considered a POD though can it? – Stewart May 07 '10 at 03:00
  • 1
    @Mark, @Stewart: A POD can have member functions, just not a **base,** copy constructor, destructor, virtual function, non-public data member, or a member failing these requirements. – Potatoswatter May 07 '10 at 03:19
2

Certainly not, especially if any of the functions in either of your classes are virtual. While the C++ standard doesn't guarantee this, having virtual functions would almost certainly change the size of your objects (because of v-tables.)

Adam Maras
  • 26,269
  • 6
  • 65
  • 91