Virtual functions and virtual base classes - I'm confused

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Winter

    Virtual functions and virtual base classes - I'm confused

    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.u k (remove [no-spam] to reply)


  • Alexander Terekhov

    #2
    Re: Virtual functions and virtual base classes - I'm confused


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

    Read up on "diamond of death".

    regards,
    alexander.

    Comment

    • Rob Williscroft

      #3
      Re: Virtual functions and virtual base classes - I'm confused

      Michael Winter wrote in news:9JVbb.2926 $SA5.26598457@n ews-
      text.cableinet. net:
      [color=blue]
      > 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?[/color]

      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) ).
      [color=blue]
      > 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.[/color]



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



      And this was the second link, the faq is well worth the read.
      [color=blue]
      >
      > Here's hoping I'll learn something new today...
      >[/color]

      Rob.
      --

      Comment

      • Peter van Merkerk

        #4
        Re: Virtual functions and virtual base classes - I'm confused

        [color=blue]
        > 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.[/color]

        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.
        [color=blue]
        > 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?[/color]

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

        --
        Peter van Merkerk
        peter.van.merke rk(at)dse.nl


        Comment

        • John Carson

          #5
          Re: Virtual functions and virtual base classes - I'm confused

          "Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in message
          news:9JVbb.2926 $SA5.26598457@n ews-text.cableinet. net[color=blue]
          > 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.u k (remove [no-spam] to reply)[/color]

          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)

          Comment

          • Chris Theis

            #6
            Re: Virtual functions and virtual base classes - I'm confused


            "Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in message
            news:9JVbb.2926 $SA5.26598457@n ews-text.cableinet. net...[color=blue]
            > 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
            >[/color]

            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


            Comment

            • Gavin Deane

              #7
              Re: Virtual functions and virtual base classes - I'm confused

              "Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in message news:<9JVbb.292 6$SA5.26598457@ news-text.cableinet. net>...[color=blue]
              > 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[/color]

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



              hth
              GJD

              Comment

              • Michael Winter

                #8
                Re: Virtual functions and virtual base classes - I'm confused

                "Gavin Deane" <deane_gavin@ho tmail.com> wrote in message
                news:6d8002d0.0 309230542.5a730 c59@posting.goo gle.com...[color=blue]
                > "Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in[/color]
                message news:<9JVbb.292 6$SA5.26598457@ news-text.cableinet. net>...[color=blue][color=green]
                > > Until about 5 minutes ago, I was happy with my knowledge of[/color][/color]
                virtual[color=blue][color=green]
                > > functions - then I read "Mixing interface and functional[/color][/color]
                inheritance"[color=blue][color=green]
                > > posted by Kevin L. earlier today. All of a sudden, I found out[/color][/color]
                that[color=blue][color=green]
                > > 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)?[/color][/color]
                What[color=blue][color=green]
                > > are the advantages and disadvantages?
                > >
                > > If you can't be bothered to give a detailed explanation, a decent[/color][/color]
                web[color=blue][color=green]
                > > reference will suffice. My documentation doesn't really say[/color][/color]
                anything[color=blue][color=green]
                > > about them, which is why I'm asking here.
                > >
                > > Here's hoping I'll learn something new today...
                > >
                > > Mike[/color]
                >
                > Joy of joys, it's in the FAQ. Take a deep breath ...[/color]

                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.u k (remove [no-spam] to reply)
                [color=blue]
                > http://www.parashift.com/c++-faq-lit...heritance.html
                >
                > hth
                > GJD[/color]


                Comment

                • Marcin Vorbrodt

                  #9
                  Re: Virtual functions and virtual base classes - I'm confused

                  "Michael Winter" <M.Winter@[no-spam]blueyonder.co.u k> wrote in message news:<9JVbb.292 6$SA5.26598457@ news-text.cableinet. net>...[color=blue]
                  > 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[/color]

                  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::varia ble and
                  Derived2::varia ble. 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

                  Comment

                  • Michael Winter

                    #10
                    Re: Virtual functions and virtual base classes - I'm confused

                    "Michael Winter" wrote on 23 Sept 03:
                    [color=blue]
                    > Until about 5 minutes ago, I was happy with my knowledge of virtual
                    > functions - then I read "Mixing interface and functional[/color]
                    inheritance"[color=blue]
                    > 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)?[/color]
                    What[color=blue]
                    > are the advantages and disadvantages?
                    >
                    > If you can't be bothered to give a detailed explanation, a decent[/color]
                    web[color=blue]
                    > reference will suffice. My documentation doesn't really say[/color]
                    anything[color=blue]
                    > about them, which is why I'm asking here.
                    >
                    > Here's hoping I'll learn something new today...
                    >
                    > Mike[/color]

                    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.u k (remove [no-spam] to reply)


                    Comment

                    Working...