我有以下玩具类 A 及其子类 B:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
A():a(1){cout<<"A default constructor..."<<endl;}
A(int i):a(i){cout<<"A non-default constructor..."<<endl;}
A(const A &ao){cout<<"A copy constructor..."<<endl; a=ao.a;}
};
class B:public A
{
private:
int b;
public:
B(int i,int j):A(i),b(j){cout<<"B constructor..."<<endl;}
//B(const B &bo){cout<<"B copy constructor... "<<endl; b=bo.b;}
void print(){cout<<endl<<"class B, a: "<<a<<" b: "<<b<<endl<<endl;}
};
int main()
{
B b1(3,8);
b1.print();
B b2=b1;
b2.print();
}
我发现如果我不为B类提供拷贝构造函数,编译器会为我合成一个,而那个使用我为A类提供的拷贝构造函数。但是,如果我为B类提供拷贝构造函数,我没有显式调用基类 A 的复制构造函数(参见代码),编译器将调用类 A 的默认构造函数?这是为什么?