我有一个看起来像这样一个层次结构:非虚拟接口 - 如何调用正确的虚拟功能
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