Why having function in my class doesn't change size of this class? This info must be stored somewhere, but where?
Me think (and I tend to be wrong most of the time) that if you only declare a non-virtual function in a class but no implementation the linker will probably remove it all together.
class Toto
{
int foo();
};
M.
You can think of a member function as being just like any other function, except that it has an extra, hidden parameter that takes a pointer to the instance on which the member function was called.
For example, this:
class C
{
void f(int i) { }
};
might be implemented (at least conceptually) as:
void C_f(C* this, int i) { }
If it was a const member function, then the hidden parameter would have the type const C* instead. Note that the situation isn't nearly this simple for virtual member functions.
The sizeof(TheClass) is affected only by the data members within the class, plus the vtable if there is any, plus padding bytes if there is any. So adding a nonvirtual function to your class does not affect its size. And if the class already contains a virtual function, adding a second one would not change sizeof(TheClass) either.