Home Help Center

Multiple Inheritance problem

brianbrian Member brian whitneyOrganization: boulder research and developmentProject: MarketBank ✭✭✭
We are having an issue whereby we cannot compile using Ice-generated classes and C++ multiple inheritance.

Let me try to describe:

IceA
|
Bar---IceB


Bar derives from two Ice-generated classes, IceA and IceB. Bar will not compile, however, because each of the Ice_generated classes defines __gcClear(), __gcReachable, etc and these virtual methods clash in Bar. Using virtual base classes does not work because IceA and IceB are different classes.

This is actually a simplified version of our hierarchy and I will not go into the specifics of why we are designing this way, but is there a way around this problem assuming the above class hierarchy?

Thanks,

Brian

Comments

  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    This is not possible, you can only derive an implementation class from one single generated class.

    If your Slice classes don't have data members, then make them interfaces in Slice. Define a new interface that derives from both of them, and then implement this interface in your code.

    If your Slice classes are really classes, i.e., have data members, then you cannot use inheritance. If Slice would allow this, then languages that don't support multiple inheritance (such as Java) could not be used.
  • brianbrian Member brian whitneyOrganization: boulder research and developmentProject: MarketBank ✭✭✭
    Marc,

    Thanks for the reply. Let me be more specific...

    --> The problem arises with C++. IceA and IceB are, in fact, interfaces in slice, but are generated by slice2cpp as non-abstract classes that derive from ::Ice::Object and then define various methods like __gcClear(). Because slice2cpp generates the same virtual methods in IceA and IceB, this causes the clash problem noticed by the compiler.

    Define a new interface that derives from both of them, and then implement this interface in your code.

    --> Don't really want to get into the why of our design, but suffice to say that we need to implement our base class, Foo, from IceA and a derived class, Bar from IceB. Notice that I say "implement" in the way one would implement an interface in Java. In Java, this does not seem to be a problem.

    IceA
    |
    Foo IceB
    \ /
    Bar


    If your Slice classes are really classes, i.e., have data members, then you cannot use inheritance. If Slice would allow this, then languages that don't support multiple inheritance (such as Java) could not be used.

    --> Both Slice definitions are trivial, one-method, interfaces, but as you know, are generated in C++ as concrete classes.

    --> Does this help you understand the problem better? I suspect we cannot get around this, but thought I'd ask in case you had an idea for a workaround.
  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    You cannot derive your implementation class from two Slice classes. Instead, you must create a new Slice class that derives from the two Slice classes, and derive an implementation from this Slice class. In this case, it doesn't matter if you also derive from the implementations of the two other Slice classes that form the base of the Slice class you implement. All that matters is that you only implement one Slice class.

    For your example, the inheritance graph could look like this:
    IceA     IceB
     |  \    /
    Foo  IceC
      \   /
       Bar
    

    You must use virtual inheritance everywhere for this to work.
  • brianbrian Member brian whitneyOrganization: boulder research and developmentProject: MarketBank ✭✭✭
    Marc,

    I fixed my problem. Thanks for you help.

    Appreciate it.

    Brian
Sign In or Register to comment.