損失

2012-01-20 16 views
2

可能性の重複:
Why does an overridden function in the derived class hide other overloads of the base class?損失

私はテンプレートパラメータとして使用されるクラスに継承されたメソッドを使用しての問題を抱えています。以下のように:

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) 

をしかし、私はボイドF(INT){}メソッドを削除した場合、コンパイラは、元のメソッドfを見つけます(D &d)。元のメソッドが新しいメソッドによって隠されているように見えます。私は理由を知りたい。

+1

をhttp://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.8を見てください –

答えて

1

はい実際には、方法B.fA.fを隠します。

class B: public A 
{ 
public: 
    using A::f; 
    void f(int) {}; 
}; 
0

派生クラスのメンバーはは、同じ名前を持つ任意の基本クラスのメンバーを非表示になります:あなたはそれがBで利用できるようにしたい場合は、usingディレクティブを必要としています。 BA::fを利用できるようにするためには、使用して宣言が必要です。

class B: public A 
{ 
public: 
    using A::f;   // <--- add this 
    void f(int) {}; 
};