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

思维导图

相关推荐
冷崖7 分钟前
const 与 constexpr
c++·学习
枫叶丹434 分钟前
【Qt开发】多元素类控件(三)-> QTreeWidget
开发语言·数据库·c++·qt
晨非辰38 分钟前
【数据结构入坑指南】--《层序分明:堆的实现、排序与TOP-K问题一站式攻克(源码实战)》
c语言·开发语言·数据结构·算法·面试
hansang_IR1 小时前
【题解】P2217 [HAOI2007] 分割矩阵 [记忆化搜索]
c++·数学·算法·记忆化搜索·深搜
洲覆1 小时前
Redis 驱动适配 Reactor 模式
开发语言·网络·数据库·redis
fl1768311 小时前
基于matlab实现的DnCNN网络
开发语言·matlab
第二层皮-合肥1 小时前
如何设置等长的最大走线长度
服务器·开发语言·php
掘根1 小时前
【Protobuf】proto3语法详解1
开发语言·前端·javascript
Lee_yayayayaya1 小时前
《通信之道—从微积分到5G》阅读笔记
开发语言·matlab
普密斯科技1 小时前
图像尺寸测量仪应用Type-C接口:精准检测,赋能科技
c语言·开发语言·科技