構造,複製,賦值,析構

#include<iostream>
using namespace std;

class A{
public:
	A(int t=0):data(t){cout<<"constructor!"<<endl;}
	//explicit A(int t=0):data(t){cout<<"constructor!"<<endl;}

	A(const A &a)
	{
		data=a.data;
		cout<<"copy constructor!"<<endl;
	}
	A & operator=(const A &a)
	{
		if(this!=&a)
		{
			data=a.data;
		}
		cout<<"operator ="<<endl;
		return *this;
	}
	virtual ~A()
	{
		cout<<"destructor!"<<endl;
	}
private:
	int data;
};

A test(const A a)
{
	return a;
}

int main()
{
	//A t;/*構造函數 析構函數*/
	
	//A t=A();/*構造函數 析構函數*/
	
	//A *t=new A();/*構造函數*/
	//delete t;/*析構函數*/
	
	//A t=1;
	//A t1=t;/*構造,複製,析構,析構*/
	
	//A t=A(1);
	//A t1;
	//t1=t;/*構造,構造,賦值,析構,析構*/


	/*如果將只有一個形參的構造函數前加explicit關鍵字,
	則阻止了這種隱式類轉化,需顯示調用構造函數*/
	//A t=1;//error
	//A t=A(1);

	A *t=new A();/*構造函數*/
	test(*t);/*複製函數,複製函數,因爲形參類型與返回值類型*/
	delete t;/*析構函數,析構,析構*/
	return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章