mardi 19 juillet 2022

Confusion regarding usage of templated base class pointer pattern

This is a regular old polymorphism design scenario except that

template<typename Params> class Base {
   Base(Params& params):
     params_(params) {
     }
   virtual void run() = 0;
   Params params_;
};

class Derived1: public Base<Derived1Params> {
   Derived1(Derived1Params& params):
    Base(params) {
   }
   void run() {
   }
};

In the above situation, I cannot use the regular old, "use base_ptr to point to derived object" technique for run-time polymorphism, due to the templates, like below

Base* bp; // An error!!!
Derived1 dr;
bp = &dr;

What is the best pattern for this scenario?

Aucun commentaire:

Enregistrer un commentaire