小编C. *_*air的帖子

派生类中的函数与基类中的完全相同,但默认参数不同

在这篇文章中读到,您可以使用其他默认参数覆盖虚拟函数,但这样做并不明智。我的情况与那里的问题略有不同。我的基类中的函数和派生类中的函数应该完全相同,只是默认参数不同。我应该如何实现这一目标?

用某种代码示例编辑我认为可能有效但违反前面提到的帖子中所说的内容,对我来说似乎不太整洁:

class base {
public:
    //not sure about the virtual here
    virtual void func(bool something = true) { /*Do some stuff*/};
};

class derived : public base {
public:
    void func(bool something = false) override
    {
        base::func(something); /*Doesn't seem like the way to go for me */
    }
};
Run Code Online (Sandbox Code Playgroud)

c++ virtual inheritance

3
推荐指数
1
解决办法
141
查看次数

标签 统计

c++ ×1

inheritance ×1

virtual ×1