CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Posts
    293

    Question Question regarding virtual inheritance

    Hi,
    I have the following classes:

    Code:
    class A
    {
    public:
    virtual bool	ZeroBalance() const			= 0;
    virtual BALANCE	operator=(const BALANCE lAmount)	= 0;
    };
    
    class B : public A
    {
    public:
    virtual void	Reserve(const BALANCE lAmount)             = 0;
    virtual void	Commit()				= 0;
    };
    
    class C : public A
    {
    virtual bool               ZeroBalance() const		        { return false; }
    virtual BALANCE	operator=(const BALANCE lAmount)   { return 0; }
    };
    
    and I need a new class:
    class D : public C, public B
    {
    virtualvoid	               Reserve(const BALANCE lAmount)	{ }
    virtual void	Commit()				{ }
    };
    it gives me compilation errors, so I added "virtual" in class B and in class C and still it's not good, it claims ambiguity problems...How can I solve this ??

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Question regarding virtual inheritance

    Class A has pure virtual functions that need to be implemented by the inherting class. Class B, same story. Class B doesn't implement the pure virtual functions from class A. Class C is implementing the pure virtual functions from Class A correctly. Class D inherits class C and B. That's not possible because class C and B are both derived from class A, this means that there could be duplicate functions, so the compiler probably doesn 't compile it.
    Last edited by Skizmo; July 10th, 2010 at 03:27 PM.

  3. #3
    Join Date
    Dec 2004
    Posts
    293

    Re: Question regarding virtual inheritance

    I didn't implement the functionality of class A in class B because it's the same functionality as in class C which also derives class A. I wanted to reuse the code of class C.

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Question regarding virtual inheritance

    Quote Originally Posted by Shvalb View Post
    it gives me compilation errors, so I added "virtual" in class B and in class C and still it's not good, it claims ambiguity problems...How can I solve this ??
    Where did you add the virtual keyword? It would be much easier if you could just provide a complete example, instead of a code snippet.
    I compiled this using virtual inheritance under MS VS2005 and it compiled with 2 warnings:
    Code:
    main.cpp(27) : warning C4250: 'D' : inherits 'C::C::ZeroBalance' via dominance
            main.cpp(19) : see declaration of 'C::ZeroBalance'
    main.cpp(27) : warning C4250: 'D' : inherits 'C::C::operator =' via dominance
            main.cpp(20) : see declaration of 'C::operator ='
    Quote Originally Posted by Shvalb View Post
    I didn't implement the functionality of class A in class B because it's the same functionality as in class C which also derives class A. I wanted to reuse the code of class C.
    Inheritance is not the best means to achieve code reuse. Polymorphism is a means to achieve abstraction.
    Your design looks peculiar to me, though I can't tell much from class names like A, B, C. Why is B derived from A?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Question regarding virtual inheritance

    it claims ambiguity problems...
    Nope. Nothing but warnings with VC++ 8 (aka 2005).

    Code:
    // 65.cpp
    typedef int BALANCE;
    
    class A
    {
    public:
    virtual bool          ZeroBalance() const			= 0;
    virtual BALANCE  operator=(const BALANCE lAmount)	= 0;
    };
    
    class B : public A
    {
    public:
    virtual void	Reserve(const BALANCE lAmount)             = 0;
    virtual void	Commit()				= 0;
    };
    
    class C : public A
    {
    virtual bool          ZeroBalance() const	{ return false; }
    virtual BALANCE  operator=(const BALANCE lAmount) { return 0; }
    };
    
    class D : public C, public B
    {
    virtual void	Reserve(const BALANCE lAmount)	{ }
    virtual void	Commit()				{ }
    };
    Code:
    D:\Temp\65>cl 65.cpp /c /Wall
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    65.cpp
    65.cpp(20) : warning C4100: 'lAmount' : unreferenced formal parameter
    65.cpp(25) : warning C4100: 'lAmount' : unreferenced formal parameter
    65.cpp(19) : warning C4514: 'C::ZeroBalance' : unreferenced inline function has been removed
    65.cpp(20) : warning C4514: 'C::operator =' : unreferenced inline function has been removed
    65.cpp(25) : warning C4514: 'D::Reserve' : unreferenced inline function has been removed
    65.cpp(26) : warning C4514: 'D::Commit' : unreferenced inline function has been removed
    How can I solve this ??
    Just never let this ("diamond" inheritance) happen, ever. In case you run into that again, you know your design is wrong.
    Last edited by Igor Vartanov; July 11th, 2010 at 02:52 PM.
    Best regards,
    Igor

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