天天看點

C++對C的加強 總結(5)C++中的構造函數和析構函數 

構造函數總結

構造函數是c++中用于初始化對象狀态的特殊函數

構造函數在對象建立時自動被調用(預設調用),隐身調用

構造函數和普通成員函數都遵循重載規則

拷貝構造函數是對象正确初始化的重要保證

必要的時候,必須手工編寫拷貝構造函數 

構造函數的調用 

自動調用:一般情況下c++編譯器會自動調用構造函數

手動調用:在一些情況下則需要手工調用構造函數

構造函數有三種

有參構造函數、預設構造函數、拷貝構造函數

析構函數

//

class test

{

public:

//有參構造函數

test(int a)

m_a = a;

}

//無參數構造函數

test()

m_a = 0;

//四種應用場景

//指派構造函數 copy構造函數

test(const test &obj)

void print()

cout<<"m_a"<<m_a<<endl;

protected:

private:

int m_a;

};

void main66()

//括号法

test t1(10); //c++編譯器自動調用這個類的有參構造函數

t1.print();

//=

test t2 = 20; //c++編譯器自動調用這個類的有參構造函數

t2.print();

test t3 = test(30);//程式員手工的調用構造函數 進行對象初始化

t3.print();

system("pause");

構造函數用來完成對象的構造(初始化)工作,

void main81()

test88 t1;

//t1.setbuf("我是t1");

t1.seta(10);

test88 t3;

//第一種場景

//1指派構造函數和=操作是兩個不同的概念

//2  指派構造函數 copy構造函數 也是構造函數

//在這個場景之下。t2被建立,并且自動的調用copy構造 

//3 當我們沒有編寫copy構造函數(指派構造函數)的時候,c++編譯器會預設給我們提供一個copy構造函數 執行的是淺copy

test88 t2 = t1; //對象t2的初始化

cout<<t2.geta()<<endl;

//t2 = t1; //是對象的=操作

system("pasue");

void main888()

//第二種場景

test88 t2(t1);

void f ( location  p )    

cout << "funtion:" << p.getx() << "," << p.gety() << endl ; 

void playobjmain()

location a ( 1, 2 ) ; 

f ( a ) ;

class location 

location( int xx = 0 , int yy = 0 ) 

x = xx ;  y = yy ;  cout << "constructor object.\n" ;  

location( const location & p )      //複制構造函數

x = p.x ;  y = p.y ;   cout << "copy_constructor called." << endl ;  }

~location() { cout << x << "," << y << " object destroyed." << endl ; }

int  getx () { return x ; } int gety () { return y ; }

private :   int  x , y ;

} ;

// void playobjmain()

// { 

//  location a ( 1, 2 ) ; 

//  f ( a ) ;

// } 

location g()

location a(1, 2);

return a;

void main101()

location b;

b = g();

void main102()

//g() 傳回一個匿名對象

location b = g();

void main1111()

main102();

規則總結:

/*

1 當類中沒有定義任何一個構造函數時,c++編譯器會提供無參構造函數和拷貝構造函數

2 當類中定義了任意的非拷貝構造函數(無參、有參),c++編譯器不會提供無參構造函數

3 當類中定義了拷貝構造函數時,c++編譯器不會提供無參數構造函數

4 預設拷貝構造函數成員變量簡單指派

總結:隻要你寫了構造函數,那麼你必須用。

*/

//對象做函數參數

//1 研究拷貝構造 

//2 研究構造函數,析構函數的調用順序

//總結 構造和析構的調用順序

#include "iostream"

using namespace std;

class abcd 

abcd(int a, int b, int c)

this->a = a;

this->b = b;

this->c = c;

printf("abcd() construct, a:%d,b:%d,c:%d  \n", this->a, this->b, this->c);

~abcd()

printf("~abcd() construct,a:%d,b:%d,c:%d  \n", this->a, this->b, this->c);

int geta() 

return this->a;

int a;

int b;

int c;

class mye

mye():abcd1(1,2,3),abcd2(4,5,6),m(100)

cout<<"myd()"<<endl;

~mye()

cout<<"~myd()"<<endl;

mye(const mye & obj):abcd1(7,8,9),abcd2(10,11,12),m(100)

printf("myd(const myd & obj)\n");

//private:

abcd abcd1; //c++編譯器不知道如何構造abc1

abcd abcd2;

const int m;

int dothing(mye mye1)

printf("dothing() mye1.abc1.a:%d \n", mye1.abcd1.geta()); 

return 0;

int run2()

mye mye;

dothing(mye);

int run3()

printf("run3 start..\n");

abcd abcd = abcd(100, 200, 300);

//若直接調用構造函數哪

//想調用構造函數對abc對象進行再複制,可以嗎?

//在構造函數裡面調用另外一個構造函數,會有什麼結果?

//abcd(400, 500, 600); //臨時對象的生命周期

printf("run3 end\n");

int main()

//run2();

run3();

name( name &obj)

cout <<" copy constructing " << endl ;

char *pn2 = obj.getpn();

pname = (char *)malloc(strlen(pn2) +1);

if (pname!=null) strcpy(pname,pn2) ;

//pname = new char[strlen(pn)+1] ;

//if (pname!=0) strcpy(pname,pn) ;

size = strlen(pn2) ;

void main1888()

///getstatic();

//getstatic();

test3 t1(1, 3), t2(3, 4), t3(5, 65);

//test(&t2, 3, 4),

cout<<test3::getcount()<<endl;;

cout<<t3.getcount()<<endl;;

void operator=( name &obj1)

cout <<" 執行=操作" << endl ;

if (pname != null)

free(pname);

pname = null;

size = 0;

char *pn = obj1.getpn();

pname = (char *)malloc(strlen(pn) +1);

if (pname!=null) strcpy(pname,pn) ;

pname[0] = ‘m‘;

size = strlen(pn) ;

繼續閱讀