目录

关于C中的类中的特殊成员函数

关于C++中的“类中的特殊成员函数”

关于C++中的“类中的特殊成员函数”

所谓“类中的特殊成员函数”,包括构造函数、析构函数、拷贝函数、拷贝构造函数、拷贝赋值函数、移动构造、移动赋值、取地址符重载、常取地址辅重载。

他们特殊的原因是:(1)这些函数无需程序员手动定义,系统会默认提供,如果程序员手动定义了,那么系统就会取消默认提供。(2)这些函数无需手动调用,在特定情况下会自动调用,即使是程序员手动定义了这些函数。

相关代码(包含构造函数、析构函数、拷贝构造函数、拷贝赋值函数)


#include <iostream>

using namespace std;

class Per
{
private:
    //Per的私有成员:姓名、年龄、身高、体重
    string name;
    int age;
    double *high;
    double *weight;
public:
    //Per的无参构造函数
    Per():high(nullptr),weight(nullptr)
    //Per():high(new double(0)), weight(new double(0))
    {
        cout<<"Per::午餐狗"<<endl;
    }
    //Per的有参构造函数
    Per(string name,int age,double h,double w):name(name),age(age),high(new double(h)),weight(new double(w))
    {
        cout<<"Per::有餐狗"<<endl;
    }
    //Per的拷贝构造函数
    Per(const Per &other):name(other.name),age(other.age),high(new double(*(other.high))),weight(new double(*(other.weight)))
    {
        cout<<"Per::靠背狗"<<endl;
    }
    //Per的拷贝赋值函数
    Per &operator=(const Per &other)
    {
        if(this!=&other)
        {
            name=other.name;
            age=other.age;
            delete high;
            delete weight;
            high=new double(*(other.high));
            weight=new double(*(other.weight));
            cout<<"Per::靠背赋"<<endl;
        }
        return *this;

    }
    void show()
    {
        cout<<"name:"<<name<<"  "<<endl;
        cout<<"age:"<<age<<"  "<<endl;
        if(high!=nullptr)
        {
            cout<<"high:"<<*high<<"  "<<endl;
        }
        else
        {
            cout<<"high:nullptr"<<endl;
        }
        if(weight!=nullptr)
        {
            cout<<"weight:"<<*weight<<"  "<<endl;
        }
        else
        {
            cout<<"weight:nullptr"<<endl;
        }
    }
    //Per的析构函数
    ~Per()
    {
        delete high;
        delete weight;
        high=nullptr;
        weight=nullptr;
        cout<<"Per::细狗"<<endl;
    }
};
class Stu
{
    //Stu的私有成员:成绩,p1
private:
    double score;
    Per p1;
public:
    //Stu的无参构造函数
    Stu()
    {
        cout<<"Stu::午餐狗"<<endl;
    }
    //Stu的有参构造函数
    Stu(double score,string name,int age,double high,double weight):score(score),p1(name,age,high,weight)
    {
        cout<<"Stu::有餐狗"<<endl;
    }
    void show()
    {
        p1.show();
        cout<<"score:"<<score<<"  "<<endl;
    }
    //Stu的拷贝构造函数
    Stu(const Stu &other):score(other.score),p1(other.p1)
    {
        cout<<"Stu::靠背狗"<<endl;
    }
    //Stu的拷贝赋值函数
    Stu &operator=(const Stu &other)
    {
        if(this!=&other)
        {
            score=other.score;
            p1=other.p1;
        }
        cout<<"Stu::靠背赋"<<endl;
        return *this;
    }
    //Stu的析构函数
    ~Stu()
    {
        cout<<"Stu::细狗"<<endl;
    }
};

int main()
{
    //无参构造
    cout<<"这是无参构造:"<<endl;
    Stu s1;
    s1.show();
    cout<<"==================================="<<endl;

    //有参构造
    cout<<"这是有参构造:"<<endl;
    Stu s2(99,"小明",19,183.5,60);
    s2.show();
    cout<<"==================================="<<endl;

    //拷贝构造
    cout<<"这是拷贝构造:"<<endl;
    Stu s3(s2);
    s3.show();
    cout<<"==================================="<<endl;

    //拷贝赋值
    cout<<"这是拷贝赋值:"<<endl;
    s1=s2;
    s1.show();
    cout<<"==================================="<<endl;

    return 0;
}

运行结果

https://i-blog.csdnimg.cn/direct/a34c72a16dec4cffbcaf1b591b5b5d13.png

写代码时遇到的问题

https://i-blog.csdnimg.cn/direct/ea3328a3f32c42e0a08772dcc6795dd0.png


FTH: (17944): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes. ***
👆👆👆错误原文·参考机翻👇👇👇
致命:启动Qt事件循环失败。应用程序当前正在崩溃。这通常是由于之前的崩溃造成的。 

结合代码书写过程来看,问题可能是:(1)初版代码使用了浅拷贝;(2)空指针的错误解引用;(3)拷贝构造函数时错误的用了无参构造的s1。(改到现在这一版代码后错误消失了,并不确定问题出现原因,浅举三种可能的原因,望指正)

思维导图