[英]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::data
在Base
隐藏所有名为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.