C++基礎知識梳理--C++的6個默認函數


C++有六個默認函數:分別是

1、default構造函數;

2、默認拷貝構造函數;

3、默認析構函數;

4、賦值運算符;

5、取值運算符;

6、取值運算符const;

// 這兩個類的效果相同

class Person
{}

class Person
{
public:
    Person() {...}   // deafault構造函數;
    Person(const Person&) {...}  // 默認拷貝構造函數
    ~Person() {...}  // 析構函數
    Person& operator = (const Person &) {...}    // 賦值運算符
    Person *operator &() {...} // 取值運算符
    const Person *operator &() const {...} // 取值運算符const
}

 

 

 

下面這個例子展現的是默認函數的調用方式;

例:

Person.h

#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
    Person();   // deafault構造函數;
    Person(const Person&);  // 默認拷貝構造函數
    ~Person();  // 析構函數
    Person& operator = (const Person &);    // 賦值運算符
    Person *operator &(); // 取值運算符
    const Person *operator &() const; // 取值運算符const

public:
    string getName() { return sName; }
    string getCode() { return sCode; }

    void setName(string name);
    void setCode(string code);

    void printInfo();
private:
    string sName;
    string sCode;
};

#endif // PERSON_H

 

Person.cpp

#include "Person.h"

Person::Person()
{
    cout << "運行:default構造函數;" << endl;
}

Person::Person(const Person &src)
{
    cout << "運行:copy構造函數;" << endl;
    sName = src.sName;
    sCode = src.sCode;
}

Person::~Person()
{
    cout << "運行:析構函數;" << endl;
}

Person &Person::operator =(const Person &src)
{
    cout << "運行:賦值運算符;" << endl;
    sName = src.sName;
    sCode = src.sCode;
    return *this;
}

Person *Person::operator &()
{
    cout << "運行:取址運算符;" << endl;
    return this;
}

const Person *Person::operator &() const
{
    cout << "運行:取址運算符const;" << endl;
    return this;
}

void Person::setName(string name)
{
    sName = name;
}

void Person::setCode(string code)
{
    sCode = code;
}

void Person::printInfo()
{
    cout << "Name : " << sName << endl;
    cout << "Code : " << sCode << endl << endl;
}
main.cpp

#include <iostream>

#include "Person.h"

using namespace std;

int main()
{
    // 創建a
    Person a;
    a.setName("李明");
    a.setCode("0101");

    // 創建b
    Person b(a);
    b.setCode("0102");

    // 創建c
    Person c;
    c = b;
    c.setCode("0103");

    // 創建d
    Person *d;
    d = &a;
    d->setCode("0104");

    // 輸出
    a.printInfo();
    b.printInfo();
    c.printInfo();
    d->printInfo();
    return 0;
}

 

輸出結果:

 

 

只聲明一個空類而不去使用時,編譯器會默認生成:

1、default構造函數;  2、默認拷貝構造函數;  3、默認析構函數;  4、賦值運算符;

 

 

構造函數:

構造函數用於創建對象,對象被創建時,編譯系統對對象分配內存空間,並自動調用構造函數。

構造函數運行的過程是:

1、系統創建內存空間,調用構造函數;

2、初始變量表;

3、函數體部分運行;

copy構造函數:

構造函數的一種,在C++中,下面三種對象需要拷貝的情況,拷貝構造函數將會被調用。

1、 一個對象以值傳遞的方式傳入函數體

2、 一個對象以值傳遞的方式從函數返回

3、 一個對象需要通過另外一個對象進行初始化

一般來說copy構造函數被重寫是為了處理默認拷貝構造函數(bitwise copy)不能完成的情況;這些情況大多來自深拷貝和淺拷貝的區別;

析構函數:

在對象析構時被掉用,用於析構對象,釋放內存;

賦值運算符:

賦值運算符存在的意義就是可以快速簡單的用一個類對一個類進行賦值;從賦值這一點上來說同copy構造函數十分相似;可以將copy構造函數看作是構造函數和賦值運算符的組合。

注意的是:必須返回 *this,也就是做操作數的引用;

 

 如何禁用這些函數?

 所有的默認函數都是public的並且是inline的;所以希望外界不能調用的話,就將這些函數申明成private類型,但是這回出現什么后果呢?

我只能說呵呵了,試試吧。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM