Xavier
Xavier

Reputation: 9059

Can a class inherit from both an abstract class and a CRTP class?

Can a class inherit from both an abstract class and a CRTP class? Or if I inherit from a CRTP class must all classes I inherit from use CRTP?

Upvotes: 0

Views: 174

Answers (2)

Robᵩ
Robᵩ

Reputation: 168716

Yes.

class AbstractBase {
public:
  virtual ~AbstractBase() {}
  virtual void Function() =  0;
};

template<class T>
class CRTPBase {
public:
  void Function2() {}
};

class Derived : public AbstractBase, public CRTPBase<Derived> {
public:
  void Function() {}
};

int main () {
  Derived d;
  d.Function();
  d.Function2();
}

Upvotes: 2

Andy Prowl
Andy Prowl

Reputation: 126502

Can a class inherit from both an abstract class and a CRTP class?

Why not? Yes, it can.

Or if I inherit from a CRTP class must all classes I inherit from use CRTP?

Why so? No, they don't have to.

Upvotes: 3

Related Questions