in terms of memory access of that region where class resides?
A class is identical to a struct (except for default visibility of members).
As such, a class or struct always has an address that it resides at in memory. That address is determined by how the class or struct is allocated, such as is it global or local (within a function) or is it allocated dynamically from the heap.
The compiler knows the type and offset of each of the members of the class or struct and therefore the total size of the class or struct. Given the base address of the class or struct and the offset of each member, then compiler can always generate the appropriate instruction to reference each member.
1 2 3 4 5 6 7
struct foo
{ int a; // offset 0 from base of struct
int b; // offset 4 from base of struct (assumes 32 bit ints)
long c; // offset 8 from base.
}; // This is a declaration only and does not have an address or take any space
foo bar; // Now we have an object of type foo named bar that has an address and takes up space
Thanks..It is clear to me now, but one more q just for clarification...If it were class instead of struct as above would the variables in constructor be allocated memory sequentially?
would the variables in constructor be allocated memory sequentially?
A constructor is a function that is called when an object is instantiated.
To answer what I think you were asking, yes, the variables in the class are allocated sequentially subject to alignment rules for the data types and the platform.