简体   繁体   English

如何通过派生的指针在基类中调用模板成员函数

[英]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
  ...
}

Derived1::data hides all members named data in Base . Derived1::dataBase隐藏所有名为data成员。 If you want to make them visible, add this line anywhere in the body of Derived1 : 如果要使其可见,请将此行添加到Derived1主体的任何位置:

using Base::data;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何注册派生的 class 成员 function 指针与基数 class - How to register a derived class member function pointer with a base class 派生类中的C ++成员函数模板,如何从基类中调用它? - C++ member function template in derived class, how to call it from base class? 当我使用根据基类定义的成员函数指针时,编译器如何调用派生成员函数? - How does the compiler call a Derived member function, when I use a member function pointer defined in terms of the Base class? 如何通过基类指针调用派生类的虚函数 - How to call virtual function of derived class through base class pointer C ++:在另一个派生类的对象的基本指针上调用(派生的)成员函数 - C++: call (derived's) member function on base pointer of a different derived class's object 派生类指针如何调用基类函数 - how a derived class pointer can call the base class function 如何在不进行类型转换和使用多态的情况下使用基类指针来调用派生类非虚拟成员函数? - How can I call derived class non virtual member function using base class pointer without typecasting and using polymorphism? C ++通过指向基类的派生类中的指向成员函数调用 - C++ call via pointer-to-member function in a derived class from the base class 函数在派生类中但不在基类中,指向基类的指针--&gt;“不是成员”错误 - function in derived but not in base class, pointer to base --> "not a member" error 使用基类函数指针访问派生类成员函数 - Using base class function pointer to access derived class member function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM