Multiple inheritance problem

This is a discussion on Multiple inheritance problem within the C++ Programming forums, part of the General Programming Boards category; I'm having some problems with multiple inheritance, in my case where the hiearchy first splits then joins again. Ok, first ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Multiple inheritance problem

    I'm having some problems with multiple inheritance, in my case where the hiearchy first splits then joins again. Ok, first some background. I'm working on a DLL so I need pure virtual interfaces for each class. In one case I have one base class and several classes inherited from that one, call them
    Code:
    class IBase
    {
      public:
        virtual void Stuff() = 0;
    };
    
    class ISub1 : public IBase
    {
      public:
        virtual void Junk() = 0;
    };
    
    class ISub2 : public IBase
    {
      ... //etc...
    };
    The base class has a (in the DLL hidden) implementation:
    Code:
    class CBase : public IBase
    {
      public:
        virtual void Stuff()
        {
          //Implementation
        }
    };
    The sub class also has an implementation, but here lies the problem, I don't want to redefine the implementation of Stuff() but use the one defined above so I inherit from CBase too.
    Code:
    class CSub1 : public ISub1, public CBase
    {
      public:
        virtual void Junk()
        {
          //Implementation
        }
    };
    But alas the compiler does not like this and thinks CSub1 is still an abstract class and cannot be instantiated.

    So, my question is (I guess), how can I solve this? I don't want to redefine Stuff() in every subclass I derive.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,657
    Make IBase a virtual base of both CSub1 and ISub1. This eliminates ambiguity about your CSub1 having a diamond of death (which means that CSub1 contains two instances of IBase internally, either of which may have a pure virtual function).

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Could you give an example, simply inheriting from IBase too gives:
    warning C4584: 'CSub' : base-class 'IBase' is already a base-class of 'ISub'
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It also eliminates COM binary compatibility, which might not be what Magos wants.

    Try to make it explicit:
    Code:
    class CSub1 : public ISub1, public CBase
    {
    public:
      using CBase::Junk;
    };
    Edit: the example Magos is asking for:
    Code:
    class IBase {};
    
    class ISub1 : virtual public IBase {};
    
    class CBase : virtual public IBase {};
    
    class CSub1 : public ISub1, public IBase {};
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    DirectX uses this on some classes so it has to be done in some way...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I know. Have you tried the using declaration?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    yep, but I fail to see how that could work as it has nothing to do with inheritance...?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It has a lot to do with inheritance. But it indeed doesn't work.

    Strictly speaking, DirectX doesn't do this. At least I don't think any object derives from any concrete class.

    OK, what about forwarding stubs?
    Code:
    class IBase
    {
    public:
    	virtual void foo() = 0;
    };
    
    class IDerived : public IBase
    {
    public:
    	virtual void bar() = 0;
    };
    
    class CBase : public IBase
    {
    public:
    	virtual void foo()
    	{
    	}
    };
    
    class CDerived : public IDerived, CBase
    {
    public:
    	virtual void foo() { CBase::foo(); }
    
    	virtual void bar()
    	{
    	}
    };
    
    int main()
    {
    	CDerived object;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Stubs seems to work ok, and on the user side it still seems like real inheritance. I'm satisfied .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple inheritance problem please help:P
    By miguel811 in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2009, 02:55 AM
  2. Exception handling framework based on multiple inheritance
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2007, 11:17 AM
  3. Inheritance problem, help please!
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2002, 08:02 AM
  4. Multiple Inheritance Ambiguity
    By FillYourBrain in forum C++ Programming
    Replies: 21
    Last Post: 08-23-2002, 11:31 AM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21