Archived

This forum has been archived. Please start a new discussion on GitHub.

ice interfaces and multiple inheritance

I have the following Slice:
class MyBaseClass {
...
};

interface MyInterface {
...
};

And then I have the following C++ code:
class MyConcreteClass : public virtual MyBaseClass, public virtual MyInterface {
...
};

When I compile, I get ambiguous inheritance errors for the methods of Ice::Object, which I assume is because all classes defined in Slice descend from Ice::Object.

I've looked through section 4.10.6 in the online Ice manual which talks about the use of multiple inheritance, but seems to only advocate its usage in slice scripts, not c++ code. Is what I am trying to do allowed? I cant really visualize how it could work, but then again I cant even visualize how the code generated by the slice scripts would get around the problem either so I'm obviously missing something. Any ideas?

Thanks,
Nick

EDIT:
One additional question - when I try to use multiple inheritance from within the Slice scripts (like below) , I get "SomeInterface" is not a class:
interface SomeInterface {
};

class SomeClass extends SomeInterface {
};

YET ANOTHER EDIT: Nevermind on the last part...found the "implements" keyword..duh :)

Comments

  • xdm
    xdm La Coruña, Spain
    Hi Nick,

    You can only derive an implementation class from one single generated class.

    Note that you could have an interface extending any number of slice interfaces.

    See here for a similar question

    http://www.zeroc.com/forums/help-center/443-multiple-inheritance-problem.html

    Let us know if you need further clarifications on that.

    Cheers,
    José
  • Thanks for the response Jose. I ended up doing the following in the Slice:
    interface MyInterface {
        void myMethod(...);
    };
    
    class MyClass implements MyInterface {
        void anotherMethod(...);
    };
    

    And then I have the following C++ code:
    class MyClassInCPP : public virtual MyClass {
        void myMethod(...) {
            ...
        };
        void anotherMethod(...) {
            ...
        };
    };
    

    So far so good - this much compiles without any problems. But I run into problems when I try to extend the C++ class I created:
    class MyClassInCPPImpl : public MyClassInCPP {
        ...
    };
    

    Compilation yields error like this:

    'MyClassInCPPImpl ' : cannot instantiate abstract class due to following members:
    'void MyInterface ::notify(...)' : is abstract see declaration of 'MyInterface::notify'

    Even though the method is defined within MyClassInCPP.
  • Nevermind the last post...the problem turned out to be that I copied and implemented the wrong declaration out of the generated c++ code. How embarrassing...
  • xdm
    xdm La Coruña, Spain
    Hi Nick

    I'm glad you found the issue.

    You could be interesting in take a look to cpp/demo/book/simple_filesystem from Ice source distribution, it made use of c++ multiple inheritance to implemt different servatns, similar to what you are doing.

    Cheers,
    José