Hi,
Ok, I'm not sure whether I understood the principle or not. |
This part ...
TheIdeasMan wrote: |
---|
The compiler maintains tables of pointers, so that given a pointer to a derived class, it can "navigate" it's way up the inheritance tree, to find whatever it needs - base class constructors or a virtual functions higher up the tree say. |
So when a derived class constructor calls a base class constructor in it's initialiser list, the compiler looks at it's internal table of pointers (or references) to find out what the parent class is. If it is the same as what was specified in the derived constructor, it calls that base class constructor. If not, it looks to see if there is another parent class, and so on until it finds one, otherwise it is a compiler error.
The same thing happens with virtual functions.
If I understand you correctly, you are talking about accessing a derived class from a base class.
If so, this seems backwards to me: If you have a base class object, there is no derived class as part of it.
Here is a description example of what I am saying:
Imagine you have a
CPie
class, and there is a
pure virtual function called
MakePie
has parameters for
CPastry
, and various type s of ingredients such as vegetables, meat, fruit, fish etc. All these things exist as classes under the base classes
CIngredient
and
CPastry
Now, one could have this:
|
virtual void MakePie (CPastry &Pastry, CIngredient &Ingredient1, CIngredient &Ingredient2);
|
But this means that fish, fruit & meat could all mixed together which may not be want people want ! So it would probably more likely look something like this:
1 2 3
|
virtual void MakePie (CPastry &Pastry, CFruit &Ingredient1, CFruit &Ingredient2) ;
virtual void MakePie (CPastry &Pastry, CMeat &Ingredient1, CVege &Ingredient2) ;
virtual void MakePie (CPastry &Pastry, CFish &Ingredient1, CVege &Ingredient2) ;
|
So now to call the function:
1 2 3 4 5 6 7
|
CDessertPie MyPie();
CSweetPastry DessertPastry();
CApricot MyApricot();
CApple MyApple();
MyPie.MakePie (DessertPastry, MyApricot, MyApple);
|
The point with this whole thing is that the function is defined high up in the inheritance tree, but the compiler does the right thing, even when the objects are well down the tree. Also, we only have 3 functions, but we might have dozens of ingredients.
Does this clear up how it works?