类用作模板参数时丢失继承的方法
Loss of inherited method when class used as a template parameter
可能重复:
为什么派生类中的重写函数隐藏基类的其他重载?
我在用作模板参数的类中使用继承方法时遇到问题。如下所示:
class D
{
};
class A
{
public:
void f( D &d ){};
};
class B: public A
{
public:
void f( int ) {};
};
template<typename F>
class C
{
public:
void doit() { D d; f->f(d); };
F *f;
};
int main()
{
C<B> cb;
cb.doit();
}
这就是我试图编译它的原因:
g++ testtemplate.cpp
testtemplate.cpp: In member function ‘void C<F>::doit() [with F = B]’:
testtemplate.cpp:28: instantiated from here
testtemplate.cpp:21: error: no matching function for call to ‘B::f(D&)’
testtemplate.cpp:14: note: candidates are: void B::f(int)
然而,如果我删除void f(int){}方法,编译器会找到原始方法f(D&D)。看起来原始方法被新方法遮蔽了。我想知道为什么。
您可以执行此
class B: public A
{
public:
using A::f;
void f( int ) {};
};
基本上,用不同的签名重新声明基类方法会隐藏所有具有相同名称的基类方法。
B.f
确实隐藏了A.f
。如果你想让它在B
中可用,你需要一个使用指令:
class B: public A
{
public:
using A::f;
void f( int ) {};
};
派生类的成员将隐藏任何具有相同名称的基类成员。为了使A::f
在B
中可用,您需要一个using声明:
class B: public A
{
public:
using A::f; // <--- add this
void f( int ) {};
};
相关文章:
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 为什么我可以通过SubBase类的公共继承方法打印出基类的私人继承成员
- C++ 实例化新对象时不接受继承方法默认参数值
- 从基类(车辆)继承方法,从派生类(汽车)继承值,以使用 C++ 在另一个派生类(车道)中实现
- 将方法添加到与继承方法相同的子类
- 重写2个重载的继承方法c++中的一个
- 从抽象类继承方法
- C++调用模板化超类的继承方法
- 继承方法,派生方法调用的不是基
- C++派生类继承方法错误
- 调用模板函数内部的继承方法
- 企业架构师 - 从类图中的接口继承方法
- 调用继承方法-编译器错误
- 计算继承方法时出错
- 调用继承方法
- 派生类的继承方法不起作用
- 在c++中推导继承方法的父类
- Qt QTableView多重继承方法
- 主类无法从我的第二个类继承方法
- c++多重继承方法重载