1. 程式人生 > >解析c++建構函式與解構函式

解析c++建構函式與解構函式

目錄

建構函式的定義

定義:建構函式是一種特殊的成員函式,對物件進行初始化的函式。

建構函式的特點

  • 建構函式名字必須與類名相同。
  • 建構函式不具有任何型別,沒有返回值。
  • 在建立類的物件的時候,系統會自動呼叫建構函式。
  • 如果使用者沒有定義建構函式,系統會預設生成一個建構函式。

建構函式

由於建構函式的引數不同,建構函式可以進行過載,可以分為預設建構函式、轉換建構函式、拷貝建構函式。

預設建構函式

定義:不帶引數的建構函式。如果程式中不宣告建構函式,那麼系統會預設生成一個建構函式。

轉換建構函式

定義:只有一個引數的建構函式,並將其它型別轉換為類型別。
注意
1. String s1(“hello”);
2. s1 = “world”;
上面第二行是將“world”賦值給s1物件。先呼叫轉換建構函式將“world”轉換為類型別,生成一個臨時物件。再呼叫’=’將臨時物件賦值給s1物件。

拷貝建構函式

定義:只有一個引數並且引數為該類的引用的建構函式,用一個已有的物件類初始化新的同一個類的物件。
1、String.h

/*****************************************************
Copyright (C): 2017-2018  
File name    : String.h
Author       : Zhengqijun
Date         : 2017年02月11日 星期六 14時58分56秒
Description  : 類的宣告
Funcion List : 
*****************************************************/
#ifndef _STRING_H_ #define _STRING_H_ #include <iostream> using namespace std; class String { public: String(); //預設建構函式 String(char *str); //轉換建構函式 String(const String& other);//拷貝建構函式 ~String(); //解構函式 /* 呼叫建構函式需要對'='進行過載 */ String& operator
=(char *str); String& operator=(const String& other); void Display() const; //成員函式 private: char *str_; //屬性 }; #endif

2、String.cpp

/*****************************************************
Copyright (C): 2017-2018  
File name    : String.cpp
Author       : Zhengqijun
Date         : 2017年02月11日 星期六 15時01分31秒
Description  : 實現類的函式
Funcion List : 
*****************************************************/

#include <iostream>
#include <string.h>
#include "String.h"

using namespace std;

String::String()
{
    cout << "default constructor String!" << endl;
    str_ = new char['\0'];
}

String::String(char *str)
{
    cout << "constructor String!" << endl;

    int len = strlen(str) + 1;
    str_ = new char[len];
    memset(str_, 0, len);
    strcpy(str_, str);
}

String::String(const String& other)
{
    int len = strlen(other.str_) + 1;
    str_ = new char[len];
    memset(str_, 0, len);
    strcpy(str_, other.str_);
}

String& String::operator=(char *str)
{
    int len = strlen(str) + 1;
    str_ = new char[len];
    memset(str_, 0, len);
    strcpy(str_, str);
    return *this;
}

String& String::operator=(const String& other)
{
    if(this == &other)    //用this指標來與自身進行比較
    {
        return *this;
    }

    int len = strlen(other.str_) + 1;
    delete [] str_;
    str_ = new char[len];
    memset(str_, 0, len);
    strcpy(str_, other.str_);
    return *this;
}

String::~String()
{
    cout << "destroy String!" << endl;
}

void String::Display() const
{
    cout << "str = " << str_ << endl;
}

3、Main.cpp

/*****************************************************
Copyright (C): 2017-2018  
File name    : Main.cpp
Author       : Zhengqijun
Date         : 2017年02月11日 星期六 15時10分27秒
Description  : 主函式
Funcion List : main()
*****************************************************/

#include <iostream>
#include "String.h"

using namespace std;

int main()
{
    String s1("hello");    //呼叫轉換建構函式對s1進行初始化
    s1.Display();

    String s2(s1);         //呼叫拷貝建構函式將物件s1賦值給物件s2
    s2.Display();

    return 0;
}

執行後輸出的結果是:

constructor String!
str = hello
str = hello
destroy String!
destroy String!

解構函式的定義

定義:解構函式也是一種特殊的成員函式,當物件的生命期結束時,對記憶體進行清理工作。

解構函式的特點

  • 當物件的生命期結束時,系統自動呼叫解構函式。
  • 解構函式名與類名相似,只是多了一個’~’。
  • 解構函式沒有引數,沒有返回值。
  • 如果沒有定義解構函式,系統會預設生成一個解構函式。
  • 解構函式沒有引數,所以不能被過載。

解構函式

注意:解構函式的呼叫順序正好與建構函式的呼叫順序相反。先構造的後析構,後構造的先析構。相當於棧 -> 先進後出。