C++基模板类虚拟方法未出现在派生中?
C++ base template class virtual methods doesn't appear in derived?
以免考虑以下代码:
#include <iostream>
template <typename T_VAL> // not used template argument
struct Foo { int x; };
template <typename T_VAL>
struct Bar { int x; };
template <typename T_VALUE>
struct A
{
// just 2 overloaded data() members:
virtual void data(Foo<T_VALUE>) {
std::cout << "FOO EMPTYn";
}
virtual void data(Bar<T_VALUE>) {
std::cout << "BAR EMPTYn";
}
};
template <typename T_VALUE>
struct B : public A<T_VALUE>
{
void data(Foo<T_VALUE>) {
std::cout << "smart FOO impln";
}
};
int main(void) {
B<float> TheObject;
Bar<float> bar;
TheObject.data(bar); // I expect virtual base call here
return 0;
}
编译器消息为:
tmpl.cpp: In function 'int main()':
tmpl.cpp:43:14: error: no matching function for call to 'B<float>::data(Bar<float>&)'
TheObject.data(bar);
^
void data(Bar<T_VALUE>)
-> void data(Bar<T_VALUE>&)
不会改变任何东西
为什么派生的类 B 没有虚拟空数据(Bar&)?
另一个data
是隐藏的。
做
template <typename T_VALUE>
struct B : public A<T_VALUE>
{
using A<T_VALUE>::data; // Add this line
void data(Foo<T_VALUE>) {
std::cout << "smart FOO impln";
}
};
相关文章:
- 使用基类中的派生方法运行线程,而无需使用模板
- 使用回调函数从构造函数调用虚拟/派生方法的替代方法?
- 无法实例化抽象类,但类不是抽象/派生方法的参数
- 使用库中的基础级别中的派生方法
- 如何通过功能指针调用派生方法
- C++ shared_ptr从派生方法返回此值
- C 基本方法称为而不是派生方法
- 可视化C++对象被上投射到基类;无法调用派生方法
- C++ 继承函数指针,可与派生方法一起使用
- 基本方法被调用而不是派生方法
- 正在调用基方法,而不是从构造函数派生方法
- 具有显式派生方法的C++接口
- 正在调用派生方法而不是Base
- 继承方法,派生方法调用的不是基
- 使用不在基类中的派生方法
- 如何使用基指针访问派生方法
- 是否可以显式使用祖先的虚拟类方法作为派生方法?
- 中的派生方法未在基类中调用
- 从基指针调用派生方法
- 从基类调用基类中不存在的派生方法