0
也许我只是非常问我的问题给谷歌,但我没有找到我的问题的答案。我的麻烦是,我的继承构造函数调用我的默认基础构造函数,我真的不明白为什么。这是我的简化版本。为什么我的继承构造函数调用我的基础默认构造函数
实施例:
A.cpp
#include <iostream>
#include "A.h"
using namespace std;
A::A()
{
cout << "A" << endl;
}
B.cpp
#include <iostream>
#include "B.h"
using namespace std;
B::B()
{
cout << "B" << endl;
}
B::B(int x)
{
cout << "B" << x << endl;
}
Source.cpp
#include <iostream>
#include "A.h"
#include "B.h"
using namespace std;
int main() {
B * b = new B();
cout << "---" << endl;
B * b2 = new B(2);
system("PAUSE");
return 0;
}
输出:
A
B
---
A
B2
Press any key to continue . . .
我只想看看B构造函数的作用。像这样:
B
---
B2
Press any key to continue . . .
你有没有继承构造函数?此外,要成功构建“B”类型的对象,还需要调用'A'的ctor ... –
听起来像你应该重新访问[良好的C++书](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)你总是必须构造派生类的基础部分。 – NathanOliver
你没有显示你的类的定义,并且你没有声明“B”来自'A'。我想我们可以从问题标题和提供的输出中推断继承。 –