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
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.