The process starts when the compiler sees the definition for SomeClass
. Based on that definition, it builds an internal structure that contains the types of the fields in SomeClass
, and the locations of the code for the methods of SomeClass
.
When you write SomeClass foo;
the compiler finds the code that corresponds to the constructor for SomeClass
, and creates machine instructions to call that code. On the next line you write int x = foo.bar
. Here the compiler writes machine instructions to allocate stack space for an int
, and then looks at its data structure for SomeClass
. That data structure will tell it the offset in bytes of bar
from the beginning of the foo
object. The compiler then writes machine code to copy the bytes corresponding to bar
into the memory for x
. All of this machine code gets written into your executable.
Generally, the data structures representing SomeClass
and other definitions are thrown away once compilation is done. What you have left is just a set of machine instructions. Those instructions are executed when you actually run your program, so that the constructor for SomeClass
and the code to copy foo.bar
into x
are executed by the CPU without any explicit knowledge of the structure of your objects.
This is the general case. There are special cases for when you run your code under a debugger and for optimization, but this is generally what happens.