C++构造函数 拷贝构造函数 析构函数 赋值的运行次序问题

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

class Test
{
public:
	Test()
	{
		cout << "test()..." << endl;
		m_x = 0;
		m_y = 0;
	}
	Test(int x, int y)
	{
		cout << "Test(int x, int y)..." << endl;

		m_x = x;
		m_y = y;
	}
	Test(const Test & another)
	{
		cout << "Test(const Test &)..." << endl;
		m_x = another.m_x;
		m_y = another.m_y;
	}

	void operator=(const Test &another)
	{
		cout << "operatoer = (const Test &)" << endl;
		m_x = another.m_x;
		m_y = another.m_y;
	}

	void printT() {
		cout << "x = " << m_x << ", m_y = " << m_y << endl;
	}

	~Test() {
		cout << "~Test()..." << endl;
	}
private:
	int m_x;
	int m_y;
};


//析构函数调用的顺序, 跟构造相反, 谁先构造的,谁后析构。
//场景1
void test1()
{
	Test t1(10, 20);
	Test t2(t1);//Test t2 = t1;
}

//场景2
void test2()
{
	Test t1(10, 20);
	Test t2;

	t2 = t1;//=操作符
}


void func(Test t)//Test t = t1; //Test t 的拷贝构造函数
{
	cout << "func begin..." << endl;
	t.printT();
	cout << "func end..." << endl;
}

//场景3
void test3()
{
	cout << "test3 begin..." << endl;
	Test t1(10, 20);

	func(t1);

	cout << "test3 end..." << endl;
}


//场景4
Test func2()
{
	cout << "func2 begin..." << endl;
	Test temp(10, 20);
	temp.printT();

	cout << "func2 end..." << endl;

	return temp;
}//匿名的对象 = temp  匿名对象.拷贝构造(temp)

void test4()
{
	cout << "test4 being.. " << endl;
	func2();// 返回一个匿名对象。 当一个函数返回一个匿名对象的时候,函数外部没有任何
			//变量去接收它, 这个匿名对象将不会再被使用,(找不到), 编译会直接将个这个匿名对象
			//回收掉,而不是等待整改函数执行完毕再回收.
	//匿名对象就被回收。
	
	cout << "test4 end" << endl;
}

void test5()
{
	cout << "test 5begin.. " << endl;
	Test t1 = func2(); //会不会触发t1拷贝构造来   t1.拷贝(匿名)?
						//并不会触发t1拷贝,而是 将匿名对象转正 t1,
						//把这个匿名对象 起了名字就叫t1.

	cout << "test 5 end.." << endl;
}

//场景6
void test6()
{
	cout << "test6 begin..." << endl;
	Test t1;//t1已经被初始化了。

	t1 = func2(); //t1已经被初始化了,所以func2返回的匿名对象不会再次转正,而依然是匿名对象。
					//所以t1会调用等号操作符,t1.operator=(匿名对象), 然后编译器会立刻回收掉匿名对象

	t1.printT();

	cout << "test6 end.." << endl;
}


int main(void)
{
	//test1();
	/* test1运行结果
	 * Test(int x, int y)...
	 * Test(const Test &)...
	 * ~Test()...
	 * ~Test()...
	*/
	
	
	//test2();
	/* test2运行结果
	 * Test(int x, int y)...
     * test()...
	 * operatoer = (const Test &)
	 * ~Test()...
	 * ~Test()...
	 */ 
	
	
	
	//test3();
	/* test3运行结果
	 * test3 begin...
	 * Test(int x, int y)...
	 * Test(const Test &)...
	 * func begin...
	 * x = 10, m_y = 20
	 * func end...
	 * ~Test()...
	 * test3 end...
	 * ~Test()...
	 */
	 
		
	//test4();
	/* test4的运行结果
	 * test4 being..
	 * func2 begin...
     * Test(int x, int y)...
	 * x = 10, m_y = 20
	 * func2 end...
	 * Test(const Test &)...
	 * ~Test()...
	 * ~Test()...
	 * test4 end
	 */
	
	//test5();
	/* test5的运行结果
	 * test 5begin..
	 * func2 begin...
	 * Test(int x, int y)...
	 * x = 10, m_y = 20
	 * func2 end...
	 * Test(const Test &)
	 * ~Test()
	 * test 5 end..
	 * ~Test()...
	 */
	 
	test6();
	/* test6的运行结果
	 * test6 begin...
	 * test()...
	 * func2 begin...
	 * Test(int x, int y)...
	 * x = 10, m_y = 20
	 * func2 end...
	 * operatoer = (const Test &)
	 * ~Test()...
	 * x = 10, m_y = 20
	 * test6 end..
	 * ~Test()...
	 */

	return 0;
}
发布了30 篇原创文章 · 获赞 58 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/annjeff/article/details/90728977