0

I have something similar to the following structure in my project.

class ProgrammersCacluator {
  public:
    virtual int add(int a, int b);
    virtual int rshift(int a, int b);
}

class MathematiciansCalculator {
  public:
    virtual int add(int a, int b);
    virtual int multiply(int a, int b);
}

I am implementing these as follows:

class ProgrammersCalculatorI : public virtual ProgrammersCalculator {
  public:
    int add(int a, int b);
    int rshift(int a, int b);
}

int ProgrammersCalculatorI::add(int a, int b) {
  return(a + b);
}

int ProgrammersCalculatorI::rshift(int a, int b) {
  return(a >> b);
}

class MathematiciansCalculatorI : public virtual MathematiciansCalculator {
  public:
    int add(int a, int b);
    int multiply(int a, int b);
}

int MathematiciansCalculatorI::add(int a, int b) {
  return(a + b);
}

int MathematiciansCalculatorI::multiply(int a, int b) {
  return(a * b);
}

Now I realize that this is a lot of extra syntax, but the majority of that is enforced by ICE (Internet Communications Engine), which is the framework we are using to communicate between sections of the project.

What I am specifically concerned about is the duplication of the add function. I tried multiple inheritance, but that (obviously) did not work.

Is there a way to adjust the structure of ProgrammersCalculatorI and MathematiciansCalculatorI such that the add method need only be implemented once?

In the actual project add is several hundred lines long and there are several methods like it.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Dan Grahn
  • 9,044
  • 4
  • 37
  • 74
  • Have a "HumansCalculator" interface that provides virtual functions for all of the operations that are common between mathematicians and programmers? – Chad Nov 25 '13 at 20:08

1 Answers1

1

You would have to make ProgrammersCacluator and MathematiciansCalculator to inherit from same base in slice, something like this:

interface BaseCacluator {
    idempotent int add(int a, int b);
};

interface ProgrammersCacluator extends BaseCalculator {
    idempotent int rshift(int a, int b);
};

interface MathematiciansCalculator extends BaseCalculator {
    idempotent int multiply(int a, int b);
};

Then you can use multiple inheritance to implement add() only once and inherit that implementation:

class BaseCalculatorI : virtual public BaseCalculator {
public:
    virtual int add( int a, int b, const Ice::Current & );
};

class ProgrammersCalculatorI : public BaseCalculatorI, virtual public ProgrammersCalculator {
public:
    virtual int rshift( int a, int b, const Ice::Current & );
};
Slava
  • 43,454
  • 1
  • 47
  • 90
  • How would that look in slice? I can't find any documentation on inheritance. I was under the impression it wasn't supported. – Dan Grahn Nov 25 '13 at 20:41
  • @screenmutt added example on slice, of course it supports inheritance http://doc.zeroc.com/display/Ice/Interface+Inheritance – Slava Nov 25 '13 at 20:48
  • Unfortunately this is an implementation of inheritance of interfaces. This does not apply to the implementation. – Dan Grahn Nov 26 '13 at 14:43
  • @screenmutt I am afraid you do not understand, with such inheritance of interface you can use inheritance in implementation, but you do not have to. But without it you cannot. See update in the answer – Slava Nov 26 '13 at 15:15
  • Ok, so this is half the answer. How does this play out in the C++? I have been working on different inheritances, which basically look like the above. None seem to work without forwarding. – Dan Grahn Nov 26 '13 at 15:16
  • @screenmutt pay attention to inheritance, where it is virtual and where it is not – Slava Nov 26 '13 at 15:22
  • Thanks! I don't know why I couldn't get that working. I gave you an upvote too. – Dan Grahn Nov 26 '13 at 15:29
  • @screenmutt most probably you used virtual inheritance from base interface as well. – Slava Nov 26 '13 at 15:31
  • I had it without. There may have been some other problems going on. – Dan Grahn Nov 26 '13 at 15:33