mardi 26 mars 2019

How to call template member function in the base class by the derived pointer

The following example is just a simplified skeleton of my code. My question is that why I cannot call the template member function from the derived pointer. According to my understanding, the member function of the base class usually can be also called from the derived object or pointer.

class Base {
 public:
  template<typename T> T* data() { 
    static_cast<T *>(data(dtype)); 
  }
  virtual void data(DataType dtype) = 0;
};

class Derived1 : public Base {
  public:
   void data(DataType dtype) override { ... }
};

class Derived2 : public Base {
  public:
   void data(DataType dtype) override { ... }
};

int main() {
  Base * base = new Derived1();
  ...
  Derived1 * derived1 = dynamic_cast<Derived1 *>(base);
  derived1->tempalte data<int>(); // Compiling error
  ...
}

Aucun commentaire:

Enregistrer un commentaire