Saturday, December 06, 2003

Re: Type Casting

thanks for the great explanation on casting. but i think i asked my question a bit wrongly. i was more interested in the actual placing of the objects on the stack/heap so that they can be accessed by the pointer.
basically what i was more interested in is how does the parent know who is the derived and vice versa.

consider the same example
class A
{
public:
int m_dataA;

void functionA() { std::cout << "functionA"; }
};
class B : public class A
{
public:
int m_dataB;

void functionB() { std::cout << "funtionB"; }
};

// Heap
-----------------
| int m_dataA
| int m_dataB
-----------------

i could think of two possibilities.

1.
either as you showed both A and B data members are stored in a linear fashion ie in contiguous memory locations. so when i need the entire B, the next sizeof extra B data members has to be considered and that will be B. the pointer always points to A and then based on the type cast simply considers the next memory locations. i hope i am clearer this time.

2.
for larger classes with a large amount of inheritance this linear memory location storage would be inefficient. just like a normal os memory management we could break the memory into partitions and allocate memory for objects in broken discontinous locations. then a table would be required to map various memory locations to the objects. i am just guessing all of this. this sounds inefficient but for larger classes? i know this is a bit into actual vm design / memory management, but in case you guys have any idea.

is the v-table in c++ associated with each of the data-member locations itself pointing to the parent, derived locations etc ? i guess this table really gets complicated with multiple inheritance.
i am purely guessing all of this. consider a v-table like implementation in java/c#.. where i store the data members of A along with the pointer add for B and parent classes if any, null here. similarly for B i store the add for A and null for derived classes. in such a case i can have a mix of the two above possibilities, storing data-members of a class together and yet large inherited objects are broken up.

so do any of you know what happens really? or just more info on v-tables would be great.

No comments: