Virtual functions and virtual base classes - I'm confused

Discussion in 'C++' started by Michael Winter, Sep 23, 2003.

  1. Until about 5 minutes ago, I was happy with my knowledge of virtual
    functions - then I read "Mixing interface and functional inheritance"
    posted by Kevin L. earlier today. All of a sudden, I found out that
    you can inherit using the virtual keyword:

    class A
    {
    }

    class B : public *virtual* A
    {
    }

    This is new to me and I've *never* seen it before today. I always
    assumed that a virtual base class was one that included virtual
    methods, whereas it seems class A above is such a class.

    So, my question is: what is a virtual base class and why use them?
    What is the effect of using them (performance, operation, etc)? What
    are the advantages and disadvantages?

    If you can't be bothered to give a detailed explanation, a decent web
    reference will suffice. My documentation doesn't really say anything
    about them, which is why I'm asking here.

    Here's hoping I'll learn something new today...

    Mike

    --
    Michael Winter
    M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
     
    Michael Winter, Sep 23, 2003
    #1
    1. Advertising

  2. Michael Winter wrote:
    [...]
    > So, my question is: what is a virtual base class and why use them?


    Read up on "diamond of death".

    regards,
    alexander.
     
    Alexander Terekhov, Sep 23, 2003
    #2
    1. Advertising

  3. Michael Winter

    Chris Theis Guest

    "Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message
    news:9JVbb.2926$...
    > Until about 5 minutes ago, I was happy with my knowledge of virtual
    > functions - then I read "Mixing interface and functional inheritance"
    > posted by Kevin L. earlier today. All of a sudden, I found out that
    > you can inherit using the virtual keyword:
    >
    > class A
    > {
    > }
    >
    > class B : public *virtual* A
    > {
    > }
    >
    > This is new to me and I've *never* seen it before today. I always
    > assumed that a virtual base class was one that included virtual
    > methods, whereas it seems class A above is such a class.
    >
    > So, my question is: what is a virtual base class and why use them?
    > What is the effect of using them (performance, operation, etc)? What
    > are the advantages and disadvantages?
    >
    > If you can't be bothered to give a detailed explanation, a decent web
    > reference will suffice. My documentation doesn't really say anything
    > about them, which is why I'm asking here.
    >
    > Here's hoping I'll learn something new today...
    >
    > Mike
    >


    Let's consider the following inheritance scheme:

    class CBase {...};

    class CChild1 : public CBase {... };

    class CChild2: public CBase { ... };

    which could come from a library for example. Now you want to create a class
    which derives from CChild1 and CChild2

    class CMyObj : public CChild1, public CChild2 {...};

    If you draw an inheritance scheme you'll see that this structure resembles a
    diamond and your class will end up with two subobjects of the CBase class.
    In case you want to upcast to a CBase object it will get tricky because the
    compiler has no way to figure out which subobject (remember you have 2 of
    them due to multiple inheritance) to use. The solution to this problem is a
    language extension in the way of providing the possibility to use virtual
    with inheritance. If you inherit from a class as virtual, only one subobject
    of that class will appear as a base class. Hence there is no ambiguity
    during upcasting.

    HTH
    Chris
     
    Chris Theis, Sep 23, 2003
    #3
  4. Michael Winter wrote in news:9JVbb.2926$SA5.26598457@news-
    text.cableinet.net:

    > Until about 5 minutes ago, I was happy with my knowledge of virtual
    > functions - then I read "Mixing interface and functional inheritance"
    > posted by Kevin L. earlier today. All of a sudden, I found out that
    > you can inherit using the virtual keyword:
    >
    > class A
    > {
    > }
    >
    > class B : public *virtual* A
    > {
    > }
    >
    > This is new to me and I've *never* seen it before today. I always
    > assumed that a virtual base class was one that included virtual
    > methods, whereas it seems class A above is such a class.
    >
    > So, my question is: what is a virtual base class and why use them?


    So that when you have a class that derives from 2 or more bases, where
    2 or more of those bases derive (vurtually from) another class that
    "other" class can be merged, so that the one we are declaring only
    containes *one* instance of the "other" base class.

    continueing from your example:

    class C: public virtual A {};

    class BC: public B, public C {};

    Assuming there is no padding/allignment we should now have:

    sizeof( BC ) == ( sizeof(B) + sizeof(C) - sizeof(A) ).

    > What is the effect of using them (performance, operation, etc)? What
    > are the advantages and disadvantages?
    >
    > If you can't be bothered to give a detailed explanation, a decent web
    > reference will suffice. My documentation doesn't really say anything
    > about them, which is why I'm asking here.


    http://www.parashift.com/c -faq-lite/multiple-inheritance.html#faq-25.11

    I put [virtual base] into the search box at:

    http://www.parashift.com/c -faq-lite

    And this was the second link, the faq is well worth the read.

    >
    > Here's hoping I'll learn something new today...
    >


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Sep 23, 2003
    #4

  5. > Until about 5 minutes ago, I was happy with my knowledge of virtual
    > functions - then I read "Mixing interface and functional inheritance"
    > posted by Kevin L. earlier today. All of a sudden, I found out that
    > you can inherit using the virtual keyword:
    >
    > class A
    > {
    > }
    >
    > class B : public *virtual* A
    > {
    > }
    >
    > This is new to me and I've *never* seen it before today. I always
    > assumed that a virtual base class was one that included virtual
    > methods, whereas it seems class A above is such a class.


    virtual inheritance pervent multiple copies a base class in a multiple
    inheritance scenario.

    For example:

    class B
    {
    };

    class D1 : public B
    {
    };

    class D2 : public B
    {
    };

    class DD : public D1, public D2
    {
    };

    Instances of class DD have essentially two instances of class B, all
    members of class B are duplicated because both D1 and D2 are derived
    from B. By changing the class definition of D1 and D2 to :

    class D1 : virtual public B
    {
    };

    class D2 : virtual public B
    {
    };

    class DD will now have only one instance of class B.

    > So, my question is: what is a virtual base class and why use them?
    > What is the effect of using them (performance, operation, etc)? What
    > are the advantages and disadvantages?


    Item 43 of the (highly recommended) book "Effective C++" from Scott
    Meyers answers your questions regarding this topic.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Sep 23, 2003
    #5
  6. Michael Winter

    John Carson Guest

    "Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message
    news:9JVbb.2926$
    > Until about 5 minutes ago, I was happy with my knowledge of virtual
    > functions - then I read "Mixing interface and functional inheritance"
    > posted by Kevin L. earlier today. All of a sudden, I found out that
    > you can inherit using the virtual keyword:
    >
    > class A
    > {
    > }
    >
    > class B : public *virtual* A
    > {
    > }
    >
    > This is new to me and I've *never* seen it before today. I always
    > assumed that a virtual base class was one that included virtual
    > methods, whereas it seems class A above is such a class.
    >
    > So, my question is: what is a virtual base class and why use them?
    > What is the effect of using them (performance, operation, etc)? What
    > are the advantages and disadvantages?
    >
    > If you can't be bothered to give a detailed explanation, a decent web
    > reference will suffice. My documentation doesn't really say anything
    > about them, which is why I'm asking here.
    >
    > Here's hoping I'll learn something new today...
    >
    > Mike
    >
    > --
    > Michael Winter
    > M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)


    Suppose that you have a base class B. Two classes, D1 and D2, both derive
    from B. Finally, we have class F that uses multiple inheritance to derive
    from both both D1 and D2.

    Ordinarily, F will contain both a D1 component and a D2 component, each of
    which will contain a B component. Thus F will contain, indirectly, two B
    components. If B has an int member x, then the value of x in the B that is
    part of D1 might be, say, 7, while the value of x in the B that is part of
    D2 might be, say, 2. If you wanted to keep the x values synchonised, you
    would have to set both whenever you set one.

    If, by contrast, virtual inheritance is used, then there is only a single B
    component within F that both D1 and D2 will share. At construction, the most
    derived class F, not D1 and D2, is responsible for calling B's constructor.

    The subject is discussed in most texts (e.g., Stroustrup, Lippman, and
    volume 2 of Bruck Eckel's Thinking in C++
    http://mindview.net/Books/DownloadSites). According to Lippman, virtual
    bases can have significant performance costs.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Sep 23, 2003
    #6
  7. Michael Winter

    Gavin Deane Guest

    "Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message news:<9JVbb.2926$>...
    > Until about 5 minutes ago, I was happy with my knowledge of virtual
    > functions - then I read "Mixing interface and functional inheritance"
    > posted by Kevin L. earlier today. All of a sudden, I found out that
    > you can inherit using the virtual keyword:
    >
    > class A
    > {
    > }
    >
    > class B : public *virtual* A
    > {
    > }
    >
    > This is new to me and I've *never* seen it before today. I always
    > assumed that a virtual base class was one that included virtual
    > methods, whereas it seems class A above is such a class.
    >
    > So, my question is: what is a virtual base class and why use them?
    > What is the effect of using them (performance, operation, etc)? What
    > are the advantages and disadvantages?
    >
    > If you can't be bothered to give a detailed explanation, a decent web
    > reference will suffice. My documentation doesn't really say anything
    > about them, which is why I'm asking here.
    >
    > Here's hoping I'll learn something new today...
    >
    > Mike


    Joy of joys, it's in the FAQ. Take a deep breath ...

    http://www.parashift.com/c -faq-lite/multiple-inheritance.html

    hth
    GJD
     
    Gavin Deane, Sep 23, 2003
    #7
  8. "Gavin Deane" <> wrote in message
    news:...
    > "Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in

    message news:<9JVbb.2926$>...
    > > Until about 5 minutes ago, I was happy with my knowledge of

    virtual
    > > functions - then I read "Mixing interface and functional

    inheritance"
    > > posted by Kevin L. earlier today. All of a sudden, I found out

    that
    > > you can inherit using the virtual keyword:
    > >
    > > class A
    > > {
    > > }
    > >
    > > class B : public *virtual* A
    > > {
    > > }
    > >
    > > This is new to me and I've *never* seen it before today. I always
    > > assumed that a virtual base class was one that included virtual
    > > methods, whereas it seems class A above is such a class.
    > >
    > > So, my question is: what is a virtual base class and why use them?
    > > What is the effect of using them (performance, operation, etc)?

    What
    > > are the advantages and disadvantages?
    > >
    > > If you can't be bothered to give a detailed explanation, a decent

    web
    > > reference will suffice. My documentation doesn't really say

    anything
    > > about them, which is why I'm asking here.
    > >
    > > Here's hoping I'll learn something new today...
    > >
    > > Mike

    >
    > Joy of joys, it's in the FAQ. Take a deep breath ...


    Yes, I realised this when I decided: "I really should get a copy of
    the C++ Language Standard, rather than relying on Microsoft's
    sometimes brief version of it". I looked in the FAQ for places to
    obtain it and I found the entry.

    Sorry for breaking a cardinal rule,

    Mike

    --
    Michael Winter
    M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)

    > http://www.parashift.com/c -faq-lite/multiple-inheritance.html
    >
    > hth
    > GJD
     
    Michael Winter, Sep 23, 2003
    #8
  9. "Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message news:<9JVbb.2926$>...
    > Until about 5 minutes ago, I was happy with my knowledge of virtual
    > functions - then I read "Mixing interface and functional inheritance"
    > posted by Kevin L. earlier today. All of a sudden, I found out that
    > you can inherit using the virtual keyword:
    >
    > class A
    > {
    > }
    >
    > class B : public *virtual* A
    > {
    > }
    >
    > This is new to me and I've *never* seen it before today. I always
    > assumed that a virtual base class was one that included virtual
    > methods, whereas it seems class A above is such a class.
    >
    > So, my question is: what is a virtual base class and why use them?
    > What is the effect of using them (performance, operation, etc)? What
    > are the advantages and disadvantages?
    >
    > If you can't be bothered to give a detailed explanation, a decent web
    > reference will suffice. My documentation doesn't really say anything
    > about them, which is why I'm asking here.
    >
    > Here's hoping I'll learn something new today...
    >
    > Mike


    Virtual function is a function whos address is determined at runtime,
    rigth? Based not on the variable (pointer/reference type), but rather
    on the type of the actuall object. Example:

    class B {
    virtual foo() {};
    };

    class D : public B {
    virtual foo() {};
    };

    B *p = new D;
    p->foo();

    Here, even though the type on p is B pointer, it points to object of
    type D, hence D's foo is called... virtual function, determined at
    runtime.

    Now, for the virtual classes.

    Best example is dimond shaped inheritance, something like this:

    class Base {
    int variable;
    };

    class Derived1 : public Base {
    };

    class Derived2 : public Base {
    };

    class Final : public Derived1, public Derived2 {
    };

    Now, base class has a variable "variable". Both Derived1 and Derived2
    inherit from base, hence objects of type Derived1 or Derived2 "have"
    this instance variable as well. So, the class Final inherits from both
    Derived1, and Derived2. This means that class Final now has two
    variables, and their fully qualified names are: Derived1::variable and
    Derived2::variable. You DO get two copies, because you did not use
    virtual inheritance.

    Now imagine that the code gets those two changes:

    class Derived1 ; virtual public Base {};
    class Derived2 : virtual public Base {};

    Everything stays the same. Now, since Base is a virtual base class for
    ALL derived types, you can now do:

    class Final : public Derived1, public Derived2 {};

    Now, Finall class will have only ONE copy of "variable".

    Hope that makes sense.

    Read up on virtual ingeritence and dimond shaped inheritance.

    Martin
     
    Marcin Vorbrodt, Sep 23, 2003
    #9
  10. "Michael Winter" wrote on 23 Sept 03:

    > Until about 5 minutes ago, I was happy with my knowledge of virtual
    > functions - then I read "Mixing interface and functional

    inheritance"
    > posted by Kevin L. earlier today. All of a sudden, I found out that
    > you can inherit using the virtual keyword:
    >
    > class A
    > {
    > }
    >
    > class B : public *virtual* A
    > {
    > }
    >
    > This is new to me and I've *never* seen it before today. I always
    > assumed that a virtual base class was one that included virtual
    > methods, whereas it seems class A above is such a class.
    >
    > So, my question is: what is a virtual base class and why use them?
    > What is the effect of using them (performance, operation, etc)?

    What
    > are the advantages and disadvantages?
    >
    > If you can't be bothered to give a detailed explanation, a decent

    web
    > reference will suffice. My documentation doesn't really say

    anything
    > about them, which is why I'm asking here.
    >
    > Here's hoping I'll learn something new today...
    >
    > Mike


    Thanks everyone for your replies, and sorry once again for posting
    something that was already in the FAQ.

    Mike

    --
    Michael Winter
    M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
     
    Michael Winter, Sep 24, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mshetty
    Replies:
    14
    Views:
    4,507
    Mike Bland
    Sep 10, 2004
  2. Replies:
    7
    Views:
    393
    Richard
    Aug 28, 2006
  3. John Goche
    Replies:
    10
    Views:
    797
    Marcus Kwok
    Dec 8, 2006
  4. Juha Nieminen
    Replies:
    3
    Views:
    426
    Marcus Kwok
    Apr 30, 2007
  5. Replies:
    2
    Views:
    393
Loading...

Share This Page