2014-02-07 31 views
0

我不知道这是一个非常奇怪的(对我)的语言功能或编译器错误:在GCC非虚函数通过虚函数的奇怪的阴影4.7.2

#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 

回答

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"; } 
};