简体   繁体   English

如何覆盖纯虚拟方法,但仍强制子类实现相同的方法?

[英]How can I override a purely virtual method, yet still force child classes to implement the same method?

I have an abstract base class with one purely virtual method:我有一个带有一个纯虚方法的抽象基类:

class Base
{
public:
    virtual void update() = 0;
};

Is there any way I could create a class that inherits from Base , overrides the update method, but still forces its children to implement an update method?有什么方法可以创建一个继承自Base的类,覆盖update方法,但仍然强制其子级实现update方法? Like this:像这样:

class Base2 : public Base
{
public:
    void update() override
    {
        if(bSomeCondition)
        {
            update(); //Calls child's update method
        }
    }
    virtual void update() = 0; // Obviously not like this
};

I know I could create two new purely virtual methods with slightly different names in Base2 , and just override those in child classes, but I would really like to keep these method names if that would be possible.我知道我可以在Base2创建两个名称略有不同的新纯虚拟方法,并只覆盖子类中的那些,但如果可能的话,我真的很想保留这些方法名称。

I'm guessing this isn't possible?我猜这是不可能的?

You can provide a definition for a pure virtual function, just not inline.可以为纯虚函数提供定义,而不是内联。

class Base2 : public Base
{
public:
    void update() override = 0;
};

void Base2::update() // Base2 is abstract regardless of this
{
    if(bSomeCondition)
    {
        update(); 
    }
}

However, this is not useful with the current implementation of Base2::update .但是,这对Base2::update的当前实现没有用。 Because a child class must override it anyway, Base2 version will not be called unless used explicitly:因为无论如何子类都必须覆盖它,除非明确使用,否则不会调用Base2版本:

class Child: public Base2
{
public:
    void update() override
    {
        Base2::update(); //infinite recursion with such implementation
    }
};

// the other way would be to require calling it explicitly at call site

std::unique_ptr<Base2> ptr = std::make_unique<Child>();
ptr->Base2::update(); 

What you should do is to provide an implementation and another pure virtual function (possibly protected ) to be called:您应该做的是提供一个实现和另一个要调用的纯虚函数(可能是protected ):

class Base2 : public Base
{
public:
    void update() override;

protected:
    virtual void doStuff() = 0;
};

void Base2::update()
{
    if(bSomeCondition)
    {
        doStuff(); //Calls child's update method
    }
}

不,这是不可能的,在我看来它会非常难以辨认。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何强制所有派生类实现虚方法? - How to force all derived classes to implement a virtual method? 强制所有类在多级继承层次结构中实现/覆盖“纯虚拟”方法 - Force all classes to implement / override a 'pure virtual' method in multi-level inheritance hierarchy 如何覆盖继承的嵌套类中存在的虚拟方法 - How to override virtual method present in inherited Nested Classes 如何调用子方法:虚拟关键字不起作用? - How can I make the method of child be called: virtual keyword not working? 如何使用私有继承的方法重写纯虚拟方法? - How can I override a pure virtual method using a privately inherited method? 是否可以覆盖(隐藏)非虚方法,但仍然可以从子类中显式调用它? - Is it okay to override (hide) a non-virtual method but still call it explicitly from a child class? 多个基类可以具有相同的虚拟方法吗? - Can multiple base classes have the same virtual method? 我想我已经覆盖了一个虚方法,但我仍然得到:“X必须实现继承的纯虚方法Y” - I think I've overridden a virtual method but I'm still getting: “X must implement the inherited pure virtual method Y” 如果调用未重写的虚方法,如何强制编译器错误? - How can I force a compiler error if an un-overridden virtual method is called? 基类可以声明一个虚方法而不定义它吗? 仍然在派生类中定义 - Can a base class declare a virtual method but not define it? Still defined in derived classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM