我有一個看起來像這樣一個層次結構:非虛擬接口 - 如何調用正確的虛擬功能
class Base
{
public:
void Execute();
virtual void DoSomething() = 0;
private:
virtual void exec_();
};
class Derived : public Base
{
public:
//DoSomething is implementation specific for classes Derived from Base
void DoSomething();
private:
void exec_();
};
void Base::Execute()
{
// do some work
exec_(); //work specific for derived impl
// do some other work
}
void Derived::DoSomething()
{
//impl dependent so it can only be virtual in Base
}
int main()
{
Derived d;
Base& b = d;
b.Execute(); //linker error cause Derived has no Execute() function??
}
所以,問題是我怎麼叫使用這種模式執行()當我創建一個派生使用我的基類。在我的情況下,我不想直接創建Derived,因爲我有從Base派生的多個類,根據某些條件我必須選擇不同的派生類。
任何人都可以幫忙嗎?
有人請編輯nvi的標籤wiki,謝謝。 – 2011-04-18 09:04:17
嗯,什麼是'nvi'? – 2011-04-18 09:05:13
@Konrad:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface – 2011-04-18 09:05:45