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

思维导图

相关推荐
weixin_439647792 小时前
JavaScript性能优化实战:从指标到落地的全链路方案
开发语言·javascript·性能优化
William_cl3 小时前
如何优化 C# MVC 应用程序的性能
开发语言·c#·mvc
Rain_is_bad3 小时前
初识c语言————位运算符
c语言·开发语言
Rain_is_bad3 小时前
初识c语言————常规运算符及其规则
c语言·开发语言
promising-w3 小时前
TYPE-C接口,其实有4种
linux·c语言·开发语言
2501_916008893 小时前
JavaScript调试工具有哪些?常见问题与常用调试工具推荐
android·开发语言·javascript·小程序·uni-app·ecmascript·iphone
zero13_小葵司3 小时前
在不同开发语言与场景下设计模式的使用
java·开发语言·javascript·设计模式·策略模式
烦躁的大鼻嘎3 小时前
【Linux】深入探索多线程编程:从互斥锁到高性能线程池实战
linux·运维·服务器·开发语言·c++·算法·ubuntu
珹洺4 小时前
Java-Spring入门指南(十三)SpringMVC基本概念与核心流程详解
java·开发语言·spring