CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    4

    Question Virtual inheritance and constructors

    So I have three classes:
    Code:
    class Base {
      int i;
    public:
      Base(int value) : i(value) {}
    };
    
    class Derived : virtual public Base {
    public:
      Derived(int value) : Base(value) {}
    };
    
    class VeryDerived : public Derived {
      VeryDerived(int value) : Derived(value) {}
    };
    I think I misunderstand how virtual inheritance and c++ constructors interact. This all seems to be reasonable, but my compiler tells me that VeryDerived cannot compile because there's no default Base constructor. None of the articles I've found online seem to mention this issue. Can anybody clarify my misunderstanding?

    EDIT: by the way, I understand that in this limited example there's no need for virtual inheritance, but I am boiling down an issue that I'm having with something that I intend to be multiply-inherited. In my real program, I want to both make a non-multiply-inherited subclass of Derived and a multiply-inherited subclass of Derived.
    Last edited by Rossman231; February 22nd, 2009 at 01:09 PM. Reason: Clarification.

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Virtual inheritance and constructors

    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: Virtual inheritance and constructors

    With virtual inheritance, the most-derived class is responsible for constructing the virtual bases. Since you didn't explicitly call the Base(int) ctor, it's trying to find a Base(void) ctor, and none exists.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured