2017-07-12 32 views
0

我知道這個問題以前曾以某種形式提出過。但我仍然感到困惑。從抽象基類中獲取重載函數

假設我有兩個類。

class A{ 
public: 
void foo(int a, int b); 
protected: 
virtual void foo(int a) = 0; 
} 

class B : public class A{ 
void foo(int a); 
} 

現在,如果我想在類B中的公共類B中的非虛函數的可見性....我怎麼做? ......換句話說目前我能夠調用非虛擬函數以這種方式

B b; 
b.A::foo(3, 5); 

,我想避免這種情況^

,並把這個

using A::foo; 

的解決方案在公開場合,B指虛擬函數在A中......而不是非虛函數......所以我認爲這不是解決方案。

+0

'using A :: foo; '指A中的所有foo。爲一個函數選擇另一個名稱。 –

+0

修復您的設計。使用相同的名稱來處理2種不同的功能和類型是這裏真正的問題。 –

回答

1
class A{ 
public: 
    void foo(int a, int b); 
protected: 
    virtual void foo(int a) = 0; 
}; 

class B : public A{ 
public: 
    using A::foo; 
protected: 
    void foo(int a); 
}; 

void f() 
{ 
    B b; 
    b.foo(1,2); // OK 
    b.foo(3); // error: ‘virtual void B::foo(int)’ is protected within this context 
} 

作品已經過期。具有兩個參數的foo可調用,foo不具有一個參數。