c/c++教程 - 2.4.2.3~4 拷贝构造函数的调用时机,构造函数调用规则

目录

4.2.3 拷贝构造函数的调用时机

4.2.4 构造函数调用规则

相关教程


 

4.2.3 拷贝构造函数的调用时机

C++中拷贝构造函数调用时机,通常有三种情况:

  • 使用一个已经创建完毕的对象来初始化一个新对象。
  • 值传递的方式给函数参数传值。
  • 以值方式返回局部对象。

示例:

class Person
{
public:

	Person()
	{
		cout << "Person 无参构造函数调用" << endl;
	}
	Person(int a)
	{
		age = a;
		cout << "Person 有参构造函数调用" << endl;
	}
	Person(const Person& p)
	{
		age = p.age;
		cout << "Person 拷贝构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Person 析构函数调用" << endl;
	}

	int age;
};

// 1.使用一个已经创建完毕的对象来初始化一个新对象
void test01 ()
{
	Person p1(20);
	Person p2(p1);
	cout << "p2的年龄: " << p2.age << endl;
}

// 2.值传递的方式给函数参数传值
void deal(Person p)
{

}

void test02()
{
	Person p;
	deal(p);
}

// 3.以值方式返回局部对象
Person deal2()
{
	Person p;
	cout << (int*)&p << endl;
	return p;
}
void test03()
{
	Person p = deal2();
	cout << (int*)&p << endl;
}

int main() {

	//test01();
	//test02();
	test03();

	system("pause");
	return 0;
}
test01();------------------------------------------------------------------
Person 有参构造函数调用
Person 拷贝构造函数调用
p2的年龄: 20
Person 析构函数调用
Person 析构函数调用
请按任意键继续. . .
test02();------------------------------------------------------------------
Person 无参构造函数调用
Person 拷贝构造函数调用
Person 析构函数调用
Person 析构函数调用
请按任意键继续. . .
test03();------------------------------------------------------------------
Person 无参构造函数调用
0073FA60
Person 拷贝构造函数调用
Person 析构函数调用
0073FB58
Person 析构函数调用
请按任意键继续. . .

 

4.2.4 构造函数调用规则

默认情况下,C++编译器至少给一个类添加3个函数:

  • 1. 默认构造函数(无参,函数体为空)
  • 2. 默认析构函数(无参,函数体为空)
  • 3. 默认拷贝构造函数,对属性进行值拷贝

构造函数调用规则如下

  • 如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
  • 如果用户定义拷贝构造函数,C++不会再提供其他构造函数。

示例:

C++编译器会提供默认拷贝构造,对属性进行值拷贝。

class Person
{
public:

	Person()
	{
		cout << "Person 无参构造函数调用" << endl;
	}
	Person(int a)
	{
		age = a;
		cout << "Person 有参构造函数调用" << endl;
	}
	//Person(const Person& p)
	//{
	//	age = p.age;
	//	cout << "Person 拷贝构造函数调用" << endl;
	//}
	~Person()
	{
		cout << "Person 析构函数调用" << endl;
	}

	int age;
};

// 构造函数的调用规则
// 1.如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
void test01 ()
{
	Person p1(20);
	Person p2(p1);
	cout << "p2的年龄: " << p2.age << endl;
}

int main() {

	test01();

	system("pause");
	return 0;
}
------------------------------------------------------------------------------
Person 有参构造函数调用
p2的年龄: 20
Person 析构函数调用
Person 析构函数调用
请按任意键继续. . .

 

如果用户定义有参构造函数,C++不再提供默认无参构造

class Person
{
public:

	//Person()
	//{
	//	cout << "Person 无参构造函数调用" << endl;
	//}
	Person(int a)
	{
		age = a;
		cout << "Person 有参构造函数调用" << endl;
	}
	//Person(const Person& p)
	//{
	//	age = p.age;
	//	cout << "Person 拷贝构造函数调用" << endl;
	//}
	~Person()
	{
		cout << "Person 析构函数调用" << endl;
	}

	int age;
};

// 构造函数的调用规则
// 1.如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
void test01 ()
{
	Person p;		// 这句编译器会报错
	Person p1(20);
	Person p2(p1);
	cout << "p2的年龄: " << p2.age << endl;
}

 

如果用户定义拷贝构造函数,C++不会再提供其他构造函数

class Person
{
public:

	//Person()
	//{
	//	cout << "Person 无参构造函数调用" << endl;
	//}
	//Person(int a)
	//{
	//	age = a;
	//	cout << "Person 有参构造函数调用" << endl;
	//}
	Person(const Person& p)
	{
		age = p.age;
		cout << "Person 拷贝构造函数调用" << endl;
	}
	~Person()
	{
		cout << "Person 析构函数调用" << endl;
	}

	int age;
};

// 构造函数的调用规则
// 1.如果用户定义有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造
// 2.如果用户定义拷贝构造函数,C++不会再提供其他构造函数
void test01 ()
{
	Person p;		// 这句编译器会报错
	Person p1(20);	        // 这句编译器会报错
	Person p2(p1);
	cout << "p2的年龄: " << p2.age << endl;
}

 

 

 

相关教程

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值