Access More Derived Class functions/values in Curiously Recurring Template pattern

55 Views Asked by At
class SuperBase
{
   virtual void func() = 0;
};

template <typename Child>
class Base: public SuperBase
{
   void func()
   {
     cout<< static_cast<Child*>(this)->m_i;
   }
};

class Derived1: Base<Derived1>
{
   static const int m_i;
}

const int Derived1:m_i = 1;


    int main()
    {
       SuperBase *iob;
       iob = new Derived1();
       iob->func();
       return 0;
    }

Now if I want to intoduce more derived class like Derived 2, and I want to pass different value to Base like -

class Derived2: Base { //static const int m_i2; }

then I will not be able to do the same. Any design tweaks which can solve the problem ?

1

There are 1 best solutions below

0
On

func can called derived_func which would them deal with the variables, but derived_func has to be the same name across all subclasses and has to be individually programmed (they can have a default in the base class)