GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Feb-2005, 05:34
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
Angry

How compiler will determine the parent in virtual derivation


Hi

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  
Old 12-Feb-2005, 09:13
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
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  
Old 14-Feb-2005, 06:32
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
Angry

CPP / C++ / C Code:
class A { public: fun() { } };
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { }; 

D obj;
obj.fun();

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  
Old 14-Feb-2005, 06:59
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
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:
class D : virtual public B, virtual public C { };
will ensure that instances of B and C are created only once in the case that we inherit from class D, but does not directly affect how many copies of A we will have.

Hence, the following code should not compile.
CPP / C++ / C Code:
class A {
public:
  void fun() { }
};

class B : public A { };

class C : public A { };

class D : virtual public B, virtual public C { };

int main() {
  D obj;
  obj.fun();
  return 0;
}

Am I explaining this well?

Matthew
  #5  
Old 14-Feb-2005, 07:15
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
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  
Old 14-Feb-2005, 08:46
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
you referring to the this pointer?
__________________
spasms!!!
  #7  
Old 15-Feb-2005, 05:57
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
No, There is another one
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 19:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.