如果抽象基类是一个接口,是否必须在派生类构造函数中调用基类构造函数?

发布于 2024-07-08 04:26:33 字数 421 浏览 7 评论 0原文

class AbstractQuery {
    virtual bool isCanBeExecuted()=0;
public:
    AbstractQuery() {}
    virtual bool Execute()=0;
};

class DropTableQuery: public AbstractQuery {
    vector< std::pair< string, string> > QueryContent;
    QueryValidate qv;
public:
    explicit DropTableQuery(const string& qr): AbstractQuery(), qv(qr) {}
    bool Execute();
};

派生类构造函数中是否需要调用基构造函数?

class AbstractQuery {
    virtual bool isCanBeExecuted()=0;
public:
    AbstractQuery() {}
    virtual bool Execute()=0;
};

class DropTableQuery: public AbstractQuery {
    vector< std::pair< string, string> > QueryContent;
    QueryValidate qv;
public:
    explicit DropTableQuery(const string& qr): AbstractQuery(), qv(qr) {}
    bool Execute();
};

Is it necessary to call base contructor in derived class constructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世俗缘 2024-07-15 04:26:33

不,事实上,基类没有必要有一个显式定义的构造函数(尽管要确保有一个虚拟析构函数)。

因此,对于典型的界面,您可以有这样的东西:

class MyInterface {
public:
    virtual ~MyInterface() {}
    virtual void execute() = 0;
};

编辑:这是您应该有一个虚拟析构函数的原因:

MyInterface* iface = GetMeSomeThingThatSupportsInterface();
delete iface; // this is undefined behaviour if MyInterface doesn't have a virtual destructor

No, in fact for it is unnecessary for the base class to have an explicitly defined constructor (though make sure you have a virtual destructor).

So for a typical interface you could have something like this:

class MyInterface {
public:
    virtual ~MyInterface() {}
    virtual void execute() = 0;
};

EDIT: Here's a reason why you should have a virtual destructor:

MyInterface* iface = GetMeSomeThingThatSupportsInterface();
delete iface; // this is undefined behaviour if MyInterface doesn't have a virtual destructor
情仇皆在手 2024-07-15 04:26:33

显式调用基类构造函数是从不的义务,除非它有参数。 编译器会自动调用构造函数。 理论上,基类仍然有一个构造函数,但如果它不执行任何操作,编译器可能会将其优化为不存在。

It is never obligatory to explicitly call the base class constructor, unless it has parameters. The compiler will call the constructor automatically. Theoretically the base class still has a constructor, but the compiler may optimize it away into non-existence if it doesn't do anything.

热血少△年 2024-07-15 04:26:33

不,不在您提供的示例中。 在初始化派生类的任何成员之前,将按照声明基类的顺序自动调用基类的默认构造函数。

No, not in the example you provided. The base class' default constructors will be called automatically in the same order that the base classes are declared, before any member of the derived class is initialized.

罗罗贝儿 2024-07-15 04:26:33

如果基类的构造函数不需要任何参数,则不需要在派生类中调用它,因为它被作为默认构造函数调用。 但是,您需要为基类提供一个虚拟析构函数,即使它是空的。 否则编译器将生成一个默认析构函数,默认情况下该析构函数是非虚拟的。

If the base class's constructor does not need any parameters, you do not need to call it in the derived class since it is called as a default constructor. However you need to provide a virtual destructor for your base class even if it is empty. Otherwise compiler will generate a default destructor which is non-virtual by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文