相关疑难解决方法(0)

继承构造函数

为什么这段代码:

class A
{
    public: 
        explicit A(int x) {}
};

class B: public A
{
};

int main(void)
{
    B *b = new B(5);
    delete b;
}
Run Code Online (Sandbox Code Playgroud)

导致这些错误:

main.cpp: In function ‘int main()’:
main.cpp:13: error: no matching function for call to ‘B::B(int)’
main.cpp:8: note: candidates are: B::B()
main.cpp:8: note:                 B::B(const B&)

B不应该继承A的构造函数吗?

(这是使用gcc)

c++ inheritance gcc constructor

199
推荐指数
5
解决办法
19万
查看次数

标签 统计

c++ ×1

constructor ×1

gcc ×1

inheritance ×1