我有一个看起来像这样的层次结构:
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??
}
所以问题是当我使用我的基类创建派生时如何使用这种模式调用 Execute() 。在我的情况下,我不想直接创建 Derived,因为我有多个从 Base 派生的类,并且根据某些条件我必须选择不同的派生类。
谁能帮忙?