jeudi 23 août 2018

Does a pure virtual destructor suffice to make a class abstract?

hope this is not becoming a 'duplicate', as there are so many questions about (pure) virtual destructures (yes, I do know about them).

I want to build an 'interface' (-> abstract class) with a bunch of methods, which can be reimplemented, but don't need to. So I have like:

class IBase
{
public:
  virtual bool doThis() = { return true; }
  virtual bool doThat() = { return true; }
}

And I'm giving a bunch of implementations, some use doThis some use doThat. That's why the 'interface' methods are just virtual and not pure. Like:

class Derived1 : public IBase
{
public:
  bool doThis() override = { doSomething(); }
}

class Derived2 : public IBase
{
public:
  bool doThat() override = { doSomethingElse(); }
}

The problem: This IBase class is instantiable, which it must not, because it doesn't do anything...

My question(s): Does it suffice to define a pure virtual destructor virtual ~IBase() = 0 to make it uninstantiable? And/or do I need to delete the standard constructor IBase() = delete?

Maybe I eventually became code-blind for thinking about it for too long, so I'll excuse in advance. And also forgive me for newbie-mistakes, that's my first question on SE.

Aucun commentaire:

Enregistrer un commentaire