定义:
如果一个类的构造函数的第一个参数是所属类类型的引用,而其他参数都有默认值,这个构造函数就是拷贝构造函数。
函数的默认参数必须放在函数声明中,除非该函数没有函数声明。
Time.h
#ifndef _TIME_H
#define _TIME_H
#include<iostream>
using namespace std;
class Timeclass
{
public:
Timeclass()
{
cout << "调用了Timeclass默认构造函数" << endl;
}
Timeclass(Timeclass & Timeclass)
{
cout << "调用了Timeclass拷贝构造函数" << endl;
}
};
class Time
{
public:
int Hour;
int min;
int secend;
private:
int haomiao;
public:
//拷贝构造函数,第一个参数为所属类类型的引用,后面的参数都有默认值
//建议:
//第一个参数带const
//一般不要声明称explicit
Time(const Time& time, int a = 20);
//构造函数
explicit Time(int temphour, int tempmin, int tempsecend);
Time(int temphour, int tempmin);
explicit Time(int temphour);
Time();
//Timeclass cl;
};
#endif
#include"Time.h"
#include<iostream>
//拷贝构造函数,系统自动调用
using namespace std;
Time::Time(const Time& temptime, int a )
{
cout << "调用了拷贝构造函数" << endl;
}
//explicit 函数显示声明,加上他之后,函数就不会进行隐式转换,
//一般用于一个参数的构造函数,防止把一个数据隐式转换为对象
Time::Time(int temphour, int tempmin, int tempsecend)
:Hour(temphour), min(tempmin), secend(tempsecend)//参数列表,用于给成员变量初始化,这样做在分配内存时就赋值,效率更高
{
cout << "调用了Time(int temphour, int tempmin, int tempsecend)构造函数" << endl;
}
Time::Time(int temphour, int tempmin)
{
}
Time::Time(int temphour)
{
cout << "调用了Time(int temphour)构造函数" << endl;
}
Time::Time()
{
cout << "调用了默认构造函数" << endl;
}
// C++project.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include"Time.h"
using namespace std;
int main()
{
//默认情况下,成员变量是逐个拷贝的
Time mytime1(10, 15, 20);//这个调用默认构造函数(带3个参数)
Time mytime2 = mytime1;//这个调用拷贝构造函数
Time mytime3(mytime1);//这个调用拷贝构造函数
Time mytime4{ mytime1 };//这个调用拷贝构造函数
Time mytime5 = { mytime1 };//这个调用拷贝构造函数
Time mytime6(10);//这个调用默认构造函数(无参数)
mytime6 = mytime5;
cout << mytime1.Hour <<"\n" << endl;
cout << mytime1.min << "\n" << endl;
cout << mytime1.secend << "\n" << endl;
}
当进行类的拷贝时,如果没有定义拷贝构造函数,那么系统就会为我们定义一个“合成拷贝构造函数”,将成员变量进行逐个拷贝,如果成员变量是整形、浮点型这类数据,就直接进行值拷贝。果果成员变量包含类类型,就会调用该类的拷贝构造函数进行拷贝。如果自己定义了拷贝构造函数,则系统不会定义。
例如下面这段代码,Time这个类里面没有定义拷贝构造函数,系统就会定义一个合成的拷贝构造函数完成拷贝,Time类中有一个Timeclass类类型的成员变量,在拷贝这个成员变量时,就会掉用这个类的拷贝构造函数。
如果Time这个类里面定义了拷贝构造函数,Time类中有一个Timeclass类类型的成员变量,在拷贝这个成员变量时,不会掉用这个类的拷贝构造函数。
//Time.h文件
#ifndef _TIME_H
#define _TIME_H
#include<iostream>
using namespace std;
class Timeclass
{
public:
Timeclass()
{
cout << "调用了Timeclass默认构造函数" << endl;
}
Timeclass(Timeclass & Timeclass)
{
cout << "调用了Timeclass拷贝构造函数" << endl;
}
};
class Time
{
public:
int Hour;
int min;
int secend;
private:
int haomiao;
public:
Time(const Time& time, int a = 20);
//构造函数
explicit Time(int temphour, int tempmin, int tempsecend);
Timeclass cl;//类对象
};
#endif
//Time.cpp文件
#include"Time.h"
#include<iostream>
//拷贝构造函数,系统自动调用
using namespace std;
Time::Time(int temphour, int tempmin, int tempsecend)
:Hour(temphour), min(tempmin), secend(tempsecend)//参数列表,用于给成员变量初始化,这样做在分配内存时就赋值,效率更高
{
cout << "调用了Time(int temphour, int tempmin, int tempsecend)构造函数" << endl;
}
//主函数
#include <iostream>
#include"Time.h"
using namespace std;
int main()
{
//默认情况下,成员变量是逐个拷贝的
Time mytime1(10, 15, 20);//这个调用默认构造函数(带3个参数)
Time mytime2 = mytime1;//这个调用系统合成拷贝构造函数
}
还有一些情况,也会调用拷贝构造函数:
1、将一个对象作为实参,传递给非引用类型的形参。
例如:
void func(Time mytime)
{
return;
}
func(mytime);//这里就会调用拷贝构造函数
2、从一个函数中返回一个对象时,系统也会调用拷贝构造函数
这里系统会构造一个临时对象,将返回的对象的值拷贝给临时对象返回。