关于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。(改到现在这一版代码后错误消失了,并不确定问题出现原因,浅举三种可能的原因,望指正)

思维导图

相关推荐
devilnumber8 小时前
Java 递归算法 详解 + 核心要点 + 实战运用 + 避坑指南
java·开发语言·算法
unicrom_深圳市由你创科技9 小时前
哪些控制逻辑应该放在 PLC,哪些放在上位机?
c++
asdfg125896310 小时前
JavaBean是什么?怎么理解?有什么用途?
java·开发语言
dsyyyyy110110 小时前
JavaScript变量
开发语言·javascript·ecmascript
玖玥拾11 小时前
C/C++ 基础笔记(十三)继承
c语言·c++·继承
z落落11 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
allway211 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_4624462311 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash
一个梦醒了11 小时前
安装git bash选项推荐
开发语言·git·bash
ct97811 小时前
React 状态管理方案深度对比
开发语言·前端·react