![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
How compiler will determine the parent in virtual derivationHi
I have a base class A. I have derived two classes B & C from A virtually. After that I have derived Class D from B and C. Class have a function fun(). I have created One object of D say obj and when I call obj.fun() it will call fun() of A. Till now everything is fine. But what is wondering me is how compiler knows weather fun() is coming to D through B or C as both have one copy of fun() and D also will have only one. Poolan |
|
#2
|
||||
|
||||
|
I would like to answer your question, but I don't completely understand your example. Could provide more detail, including the context in which you use obj of type D and a code example?
Matthew |
|
#3
|
|||
|
|||
|
CPP / C++ / C Code:
We can call like this. right? If the virtual keyword is not there while deriving B and C from A we will have two copies of fun() in D. right? But the virtual keyword will avoi that. I would like to know how the compiler knows that the fun() is coming to D through B or C. Last edited by LuciWiz : 15-Feb-2005 at 04:51.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#4
|
||||
|
||||
|
e base class is instantiated only once in any derived class. So using virtual when inheriting from A in the intermediate classes B and C is enough to avoid instantiating multiple copies of base class A, even during the instantiation of a D object.
Placing the keyword virtual in the declaration for D, say CPP / C++ / C Code:
Hence, the following code should not compile. CPP / C++ / C Code:
Am I explaining this well? Matthew |
|
#5
|
|||
|
|||
|
Hi,
Thank you for your detailed reply. Will you clarify one more thing. I read in one book that there is one extra pointer along with the class in case of virtual derivation (Not the VPTR, which is for virtual funs, etc). Is the single time instanciation of the class is handled through this pointer. From where can I get the details about this. Can you please ref one good book for me. I am very eager to know the internals of C++. |
|
#6
|
|||
|
|||
|
you referring to the this pointer?
__________________
spasms!!! |
|
#7
|
|||
|
|||
|
No, There is another one
|
Recent GIDBlog
Toyota - 2008 July Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dealing with virtual functions | crystalattice | C++ Forum | 0 | 16-Nov-2004 22:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The