4

Possible Duplicate:
Why class size, depends only on data members and not on member functions?

When I first learnt about Inheritance, my teacher remarked that as opposed to data members, member function don't change the class size. That is, if class B inherits from class A then B's size will be larger than A's size if and only if at least one data member will be added, and won't be changed with respect to function members quantity. Is it correct? If so, How this mechanism works? It seems like both members should be held in the heap and therefore will cost in size.

Thank you, Guy

Community
  • 1
  • 1
SyndicatorBBB
  • 1,757
  • 2
  • 26
  • 44
  • "Heap" is implementation detail; there may be no heap at all. Call it dynamic storage (for "heap") and automatic storage (for "stack"), which will be always correct - even in case where heap and stack aren't actual implementations of those storage classes. – Griwes Jan 15 '13 at 21:33
  • This is NOT ALWAYS TRUE. Having one or more virtual member function in a class adds a virtual table pointer to it. So if A has no virtual member functions but B adds a virtual member function `sizeof(B)` will be greater than `sizeof(A)`. – Csq Jan 15 '13 at 21:37
  • @Csq, vptr is essentially member variable. – Griwes Jan 15 '13 at 21:38

3 Answers3

5

Member variables are stored as part of each class instance. But member functions are not.

Each instance of a class must have a separate copy of each member variable. This keeps each object unique.

But member functions are code. And no matter how many instances you have of a particular class, there is absolutely no reason to have multiple copies of the code. The same code simply operates on the instance data.

Since code is different, code is not part of the size of a class instance. And sizeof(myClass) will not include bytes occupied by code.

TyBcodar
  • 67
  • 8
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    "Method" is not a well defined term in C++ land. Proper term is "member function", which is actually *defined* in standard. But +1. – Griwes Jan 15 '13 at 21:28
2

You can imagine that calling a member function myClass.myMethod(x) is similar to myMethod(myClass, x). There is no need for that function to exist per every instance of class. You can access this "hidden" first argument using this keyword.

yattering
  • 436
  • 4
  • 7
1

if class B inherits from class A then B's size will be larger than A's size if and only if at least one data member will be added, and won't be changed with respect to function members quantity.

Yes it is correct. (if B doesn't implement other methods). Each class instance has a copy of the data members and a pointer to a member function table where they are actually stored.

The code of the member functions is shared between different instances of the same class. If B doesn't override any member function of the base class A, both A and B can share the same methods. When you override a member function in a subclass, basically you are changing this mechanism, creating a new definition of the overriden member function, available only for the subclass where you defined it (es.B).

Normally a member function is binded at compile time. So if you have an instance of the subclass B referenced through a pointer to the base class A, and you call a member function foo() defined in both classes, the function called will be the one implemented in the base class. You can force a member function to be binded at run-time declaring it virtual (the member function called will be the one of the actual type of the class pointed through the base pointer). This will cause an additional table (Virtual Method Table , vtable ) to be used to store virtual methods, and a double pointer indirection for each call.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • "Method" is not well defined term in C++ land. Proper term is "member function", which is actually *defined* in standard. Also, OP did not mention virtual functions, so I don't think that vtable reference is needed here (and, well, vtable is just another member variable). – Griwes Jan 15 '13 at 21:32