I am currently reading Inside the C++ Object Model. On page 9 it has a diagram showing how the contents of a class are laid out in memory. It states the only part of an object which actually resides in the class memory are non-static data members.
Here is a post from SO regarding the contents of memory for a program:
Global memory management in C++ in stack or heap?
In the second answer it details the memory layout of a program- showing the stack and the heap.
Does the location of the static data members/any class function (basically the parts of the class which are not stored within the object- referring to page 9) change depending whether the object is on the stack or the heap?
Static data members reside in the same area of memory that global variables and plain static variables would reside. It is the “class memory” that could either be on the stack or heap, depending on how the instance of the class was created.
A static data member is not too different from a global variable. However, it is scoped by the class name, and its access by name can be controlled via
public,private, andprotected.publicgives access to everyone.privatewould restrict access to only members of the class, andprotectedis likeprivatebut extends access to a class that inherits from the class with the static data member.In contrast, a global variable is accessible by name by everyone. A plain static variable is accessible by name by code in the same source file.
A plain class method is actually just a regular function (modulo access controls), but it has an implicit
thisparameter. They do not occupy any space in a class. However, avirtualclass method would occupy some memory in the class, since it has to resolve to a derived class’s implementation of the method. But, polymorphism is likely not yet covered where you are in your textbook.