【发布时间】:2013-11-30 13:06:51
【问题描述】:
我想知道为什么人们说:
“继承类不继承构造函数”。
如果可以使用父类的构造函数,无参构造函数无论如何都会自动调用。
例子:
#include <iostream>
using namespace std;
class A {
private :
int x;
public :
A () {
cout << "I anyway use parameter-less constructors, they are called always" << endl;
}
A (const int& x) {
this->x = x;
cout << "I can use the parent constructor" << endl;
}
};
class B : public A {
private :
int y;
public :
B() {
}
B (const int& x, const int& y) : A (x) {
this->y = y;
}
};
int main() {
B* b = new B(1,2);
B* b1 = new B();
return 0;
}
那么“说”是否正确,构造函数是在 c++ 中继承的?编程语言中“继承”的确切定义是什么?
提前致谢。
【问题讨论】:
标签: c++ inheritance constructor theory