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

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

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

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

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

cpp 复制代码
#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;
}

运行结果

写代码时遇到的问题

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

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

思维导图

相关推荐
阿凤213 分钟前
uniapp如何修改下载文件位置
开发语言·前端·javascript
m0_716765233 分钟前
数据结构--循环链表、双向链表的插入、删除、查找详解
开发语言·数据结构·c++·学习·链表·青少年编程·visual studio
聆风吟º4 分钟前
【C标准库】深入理解C语言strstr函数:子字符串查找的实用指南
c语言·开发语言·库函数·strstr
XY_墨莲伊4 分钟前
【编译原理】实验一:基于正则文法的词法分析器设计与实现
开发语言·数据结构·算法
Tirzano6 分钟前
springsession全能序列化方案
java·开发语言
坐吃山猪7 分钟前
Python20_MCP添加鉴权
开发语言·python
gihigo199824 分钟前
分布式发电的配电网有功-无功综合优化 MATLAB 实现
开发语言·分布式·matlab
人工干智能25 分钟前
科普:python的pandas包中的DataFrame就是二维表
开发语言·python·pandas
浪客川26 分钟前
【百例RUST - 006】一文理解所有权和切片
开发语言·后端·rust
Westward-sun.29 分钟前
PyQt5入门实战:从零实现一个表达式输入式计算器(附完整代码)
开发语言·qt