0

I have a C++ interface with two methods. Any class implementing this interface need to implement at least one of the two methods but may define both. It is however not necessary to define both. What is the best pattern to use in such situation ?

Issam T.
  • 1,677
  • 1
  • 14
  • 32
  • In what context are you using these classes? You might want to elaborate. Take note opinion based questions are off-topic – Passer By Dec 08 '17 at 08:38
  • I think if you want a class to implement your interface you have to implement the whole interface. Do you want to overload a common implementation in specific class ? – ponayz Dec 08 '17 at 08:48
  • 3
    Split the interface into 2 and put one method in each. Classes can now implement either or both interfaces. An interface should be considered a contract (I implement all of this). – Richard Critten Dec 08 '17 at 08:53
  • 1
    I came across this [answer](https://stackoverflow.com/questions/6235924/force-derived-class-to-override-at-least-one-virtual-function) I think you might wanna check it out. – ponayz Dec 08 '17 at 08:59
  • This is **not** duplicate of [this](https://stackoverflow.com/q/6235924/1870232). I reopened it – P0W Dec 08 '17 at 09:41

2 Answers2

0

Something like following:

class MyInterface
{
public:
  virtual ~MyInterface() {}

  virtual void foo() = 0;
  virtual void bar() = 0;
};


class MyAbstractClass :  public MyInterface
{
public:
  virtual ~MyAbstractClass();

  // Provide implementation for foo or bar
  virtual void foo() override { }
  virtual void bar() override { }
};

class MyNewClass : public MyAbstractClass
{
   // Implement either of foo or bar or can be both
};
P0W
  • 46,614
  • 9
  • 72
  • 119
  • MyAbstractClass doesn't inherited from MyInterface. In anyway what's the point in MyAbstractClass why not make MyInterface method virtual instead of pure virtual ? – ponayz Dec 08 '17 at 08:56
  • 1
    @Sorry about missing MyInterface inherit. Also, OP mentioned _"I have a C++ interface with two methods"_ so I gave Interface example. – P0W Dec 08 '17 at 08:58
  • I would rather make the `foo` and `bar` in `MyInterface` non-pure virtual with default implementation doing something like "WARN: operation not supported". – pptaszni Dec 08 '17 at 10:48
  • @Ptaq666 Did you see my last comment ? OP mentioned "I have a C++ **interface** with two methods" . If OP can change his interface , there's no point of this post. – P0W Dec 08 '17 at 11:52
0

IMHO the question is already wrong and also the class it is based on.

Case A: A class has a definition for a given method. The base implementation should provide an expectable behavior. In this case you are free to override it or not.

Case B: The method is abstract so you have to overwrite it in an expectable manner, otherwise the compiler won't compile.

There is no way to declare a "please overwrite one or both" in C++. A class given, which expect that, is (very) bad design.

So the question does not exist. If it exists, it is not possible to answer it with more details, as it depends on the context.

Martin Schlott
  • 4,369
  • 3
  • 25
  • 49