Topic: memory model for classes


Author: adaga@asic.mtv.nec.com
Date: 15 Aug 94 11:42:53 -0800
Raw View
if you have a class with a bunch of functions, when you create several
objects of that instance, what's in memory?  I understand that the data
structures would have its own for each object, but what about the functions?
thanks in advance,
anthony@asic.mtv.nec.com





Author: Rey Crisostomo
Date: 16 Aug 1994 21:44:53 GMT
Raw View
In article <1994Aug15.114254.7@asic.mtv.nec.com> adaga@asic.mtv.nec.com writes:
>if you have a class with a bunch of functions, when you create several
>objects of that instance, what's in memory?  I understand that the data
>structures would have its own for each object, but what about the functions?

In the simplest case, each instance of a class (i.e. each object) only contains the
data members defined in the class. Member functions are _not_ replicated for
each object. Everytime a non-static member function is called for an object, that
member function receives an invisible parameter (named "this") which is the pointer
to the object for which the member function was called. This is how each member
function know which object in memory it is working on. Inside each non-static member
function, every reference to non-static data members and member functions are
implicitly prefixed with "this->".

Static data members and static member functions are a different case.

If a class has virtual function(s) or virtual base classes, some additional
information, which is invisible to the programmer, is added to the object in memory.

Hope that helps,

Rey Crisostomo
branzrpc@branz.org.nz
Wellington, New Zealand