I have an abstract class IExecutable

class IExecutable
{
public:
    virtual ActionStatus Execute() = 0;
};

With an intermediate class Action

class Action : public IExecutable
{
    virtual ActionStatus Execute();
};

which implements Execute(), and other final classes, such as GoTo

class GoTo : public Action
{
    ...
};

GoTo does not implement Execute(). The compiler sees GoTo as an abstract class because it doesn't implement Execute().

Two questions: Isn't the fact that Action implements Execute() enough so that GoTo shouldn't need to implement it? And if not, how would that be fixed? It doesn't make sense to have to reimplement Execute() for every final class when it's the same for all final Actions.

有帮助吗?

解决方案

It sounds like this isn't the whole story. GoTo will be abstract if there is any pure virtual function without implementation somewhere up the hierarchy.

In C++11, you can ensure that Execute is properly implemented using the override keyword:

virtual ActionStatus Execute() override;

This will cause a specific diagnostic if you accidentally declare a new virtual function instead of an override.

You can specify that GoTo is not the base of anything using final, which may cause the compiler to complain if it is abstract (but this may not be required):

class GoTo final : public Action

In any case, the compiler should tell you what function was not implemented to help you track down this kind of bug.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top