C++的六个默认函数

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类型,但是这回出现什么后果呢?

我只能说呵呵了,试试吧。

 

猜你喜欢

转载自blog.csdn.net/qq_35886593/article/details/88688609