vendredi 23 juillet 2021

Why using a templated struct as a base class instead of a regular class/interface? [duplicate]

I'm working with a piece of code that takes as base class a templated struct:

template <typename T>
struct myStruct
{
  size_t getSize() const noexcept { return static_cast<const T &>(*this).getSize(); };
  // A bunch of similar functions
}

The class inheriting from this struct looks as follows:

class myClass : public myStruct<myClass>
{
  size_t getSize() const noexcept;
}

As far as I understand, the implementation of the base class ends up calling the function of the derived one. Furthermore, the base class is not referred in the code I'm working with, and all objects I interact with are instances of the derived class.

My question is: is there any benefit in using such struct as base class, instead of using regular inheritance (without templates)? Or, why using inheritance in such scenario?

Aucun commentaire:

Enregistrer un commentaire