我想知道这是一个非常奇怪的(对我来说)语言功能还是编译器错误:
#include <iostream>
template<class T>
class A{
public:
virtual void func(T const & x)
{ std::cout << "A func: " << x << "\n"; }
void func(T const &x, T const & y)
{ std::cout << "Double func:\n";
func(x); func(y);
}
};
template<class T>
class B : public A<T>{
public:
virtual void func(T const & x)
{ std::cout << "B func: " << x << "\n"; }
};
int main(){
A<int> a;
a.func(1);
a.func(2,3);
B<int> b;
b.func(1);
b.func(2,3);
}
a.func(1)和a.func(2,3)都可以正常工作。但是b.func(2,3)产生:
3.c++: In function ‘int main()’:
3.c++:27:13: error: no matching function for call to ‘B<int>::func(int, int)’
3.c++:27:13: note: candidate is:
3.c++:20:16: note: void B<T>::func(const T&) [with T = int]
3.c++:20:16: note: candidate expects 1 argument, 2 provided
答案 0 :(得分:1)
它不叫阴影,但隐藏,是的,它是一种语言功能。
您可以使用using
指令使基本功能可用:
template<class T>
class B : public A<T>{
public:
using A<T>::func; // <----------------
virtual void func(T const & x)
{ std::cout << "B func: " << x << "\n"; }
};