c++ 构造函数什么时候调用

1.概要

1.构造函数什么时候调用?
只有对被创建的时候才会创建构造函数,创建对象有两种情况,一种是直接构造,一种是拷贝构造,无论哪种都可以创建对象,且也必须是通过这两个函数才能创建对象;真正产生的对象数等于这两个函数被调用的次数和,这两个函数被调用过多少次,就一定会有多少个对象。


2.赋值运算符什么时候调用?
指针赋值的时候会调用了?不会。只有指针赋值的时候才会调用。

3.指针的本质是保持地址,指针直接的赋值就是保持的地址的改变,所以发生指针赋值,并不会对对象本身有任何影响,只是指针和对象的关系发生了变化。

2.代码

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
using namespace std;

class A
{
public:
	A() {
		cout << "Call constructor\n";
	}
	A(const A& a) {
		cout << "Copy constructor call\n";
		this->a = a.a;
	}
	~A() {
		cout << "this:" <<this<<"\n";
		cout << "Call the destructor\n";
		
	}
	A& operator= (const A& a){
		cout << "Assignment operator call\n";
		this->a = a.a;
		return *this;
	}
private:
	int a = 0;
};
namespace t1 {
	void test() {
		cout << "Normal condition ------------------------------------\n";
		try
		{
			A* a = new A();
			A* b = new A(*a);
			delete a;
			delete b;
		}
		catch (const std::exception&)
		{

		}
	}
/* 运行结果:
Normal condition ------------------------------------
Call constructor
Copy constructor call
this:0000023B9C31B540 //两次释放的真正内存地址不同 delete a
Call the destructor
this:0000023B9C31B000 //两次释放的真正内存地址不同 delete b
Call the destructor
Abnormal condition: Special treatment */
}
namespace t4 {
	void test() {
		cout << "Normal condition ------------------------------------\n";
		try
		{
			A* a = new A();
			A* b = new A(*a);
			//a = b;//单纯指针赋值不会调用赋值运算符
			*a = *b;// 赋值运算符会被调用
			delete a;
			delete b;
		}
		catch (const std::exception&)
		{

		}
	}
	/*
Normal condition ------------------------------------
Call constructor
Copy constructor call
Assignment operator call //赋值运算符会被调用
this:000001E5179AB6C0
Call the destructor
this:000001E5179AAE40
Call the destructor*/
}
namespace t2 {
	void test() {
		//异常情况 进行了特殊处理
		cout << "Abnormal condition: Special treatment ------------------------------------\n";
		try
		{
			A* a = new A();
			A* b = new A(*a);
			//两指针的
			//加了这行语言,两个指针地址相同,释放内存的时候会有问题
			delete a;//为了不报异常,这里先把内存释放了,其实这里不释放也不会有问题,但是会有指针悬垂
			a = b;
			delete a;
			//delete b;//因为目前两个指针指向的都是对象*b,所以第一个delet 已经释放了b的只是,这里重复操作会报异常。
		}
		catch (const std::exception&)
		{

		}
	}
/* 运行结果:
Abnormal condition: Special treatment------------------------------------
Call constructor
Copy constructor call
this:0000023B9C31ACC0 //两次释放的真正内存地址不同 delete b
Call the destructor
this:0000023B9C31A9C0 //两次释放的真正内存地址不同 delete a
Call the destructor
*/
}
namespace t3 {
	// 异常情况发生
	void test() {
		cout << "Abnormal condition ------------------------------------\n";
		try
		{
			A* a = new A();
			A* b = new A(*a);
			//两指针的
			//加了这行语言,两个指针地址相同,释放内存的时候会有问题
			a = b;//这时候a 和 b 的指针都指向 (*b)
			delete a;//==delete b
			delete b;//==delete b 这里又一次 delete b 所以会报异常
		}
		catch (const std::exception&)
		{

		}
	}
/* 运行结果:
Abnormal condition ------------------------------------
Call constructor
Copy constructor call
this:0000023B9C31B080 //两次释放的真正内存地址相同 delete b(a<-b)
Call the destructor
this:0000023B9C31B080 //两次释放的真正内存地址相同 delete b
Call the destructor*/
}
int main()
{
	t1::test();
	t4::test();
	t2::test();
	t3::test();
    cout << "Hello World!\n";
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

3.运行结果

Normal condition ------------------------------------
Call constructor
Copy constructor call
this:000001D5D053AC80
Call the destructor
this:000001D5D053B1C0
Call the destructor
Normal condition ------------------------------------
Call constructor
Copy constructor call
Assignment operator call
this:000001D5D053B1C0
Call the destructor
this:000001D5D053B040
Call the destructor
Abnormal condition: Special treatment ------------------------------------
Call constructor
Copy constructor call
this:000001D5D053AEC0
Call the destructor
this:000001D5D053B440
Call the destructor
Abnormal condition ------------------------------------
Call constructor
Copy constructor call
this:000001D5D053AD40
Call the destructor
this:000001D5D053AD40
Call the destructor





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值