如果基类有私有成员,并且需要在派生类中的构造函数中初始化。这是会用到基类的复制构造函数。
有如下示例:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class base{
private:
int i;
public:
base() :i(0)
{
cout << "base constructer.i = "<< i << endl;
}
base(int n) :i(n)
{
cout << "base(n) constructer.i = " << i << endl;
}
base(const base &n) :i(n.i)
{
cout << "base&n constructer.i = " << i << endl;
}
};
class test : public base{
private:
int j;
public:
test() :base(0), j(0) { cout << "test constructer .j = " << j << endl; }
test(int i, int j) :base(i), j(j) { cout << "test(i,j) constructer .j = " << j << endl; }
test(const test &obj) :base(obj), j(obj.j) { cout << "test(const test&obj) constructer .j = " <<