C++拷贝构造和移动构造函数小实验

简介

自己些C++代码时,总是使用最简单的C++语法,一般不会用到拷贝构造和移动构造这些比较高级的语法。但是,在读别人的C++代码时,总是被弄得晕头转向。因此,也简单学习一下。正所谓知己知彼,百战不殆。

试验代码

#include <iostream>                                                                                                                                             

using namespace std;

class Class {

    public:
        Class() : a(""), b(""), c("") {
            d = new int(0);
            cout << "***      construct: " << hex << this << endl;
        }

        Class(const Class &cls) : a(cls.a), b(cls.b), c(cls.c) {
            d = new int(0);
            cout << "*** copy construct: " << hex << this << endl;
        }

        Class(Class &&cls) : a(cls.a), b(cls.b), c(cls.c), d(cls.d) {
            cls.d = NULL;
            cout << "*** move construct: " << hex << this << endl;
        }

        ~Class() {
            delete d;
            cout << "###       destruct: " << hex << this << endl;
        }

        bool compare(const Class &b) {

            cout << "*** call lvalue param" << endl;
            if(this->a == b.a) {
                return true;
            }

            return false;
        }

        bool compare(Class &&b) {

            cout << "*** call rvalue param" << endl;
            if(this->a == b.a) {
                return true;
            }

            return false;

        }

    public:
        string a;
        string b;
        string c;
        int *d;
};

Class getClass() {

    Class x;
    x.a = x.b = x.c = "test";
    x.d = new int(0x14);
    return x;
}

Class x;
Class &&getClassR() {

    x.a = x.b = x.c = "test";
    x.d = new int(0x1e);
    return std::move(x);
}

int main() {

    string test = "world";
    Class a;
    a.a = "hello";
    a.b = "world";
    a.c = "what";

    Class b(a);

    b.a = "which";                                                                                                                                              

    cout << "-------------------------------------------" << a.a << " " << a.b << " " << a.c << endl;
    cout << "-------------------------------------------" << b.a << " " << b.b << " " << b.c << endl;

    //Class   c = getClass(); //call 2 move construct
    Class &&c = getClass(); //call 1 move construct
    c.a = "lili";

    cout << "-------------------------------------------" << c.a << " " << c.b << " " << c.c << " " << *c.d << endl;

    const Class d = getClassR();
    cout << "-------------------------------------------" << d.a << " " << d.b << " " << d.c << " " << *d.d << endl;

    cout << "-------------------------------------------" << a.compare(b) << endl;
        cout << "-------------------------------------------" << a.compare(std::move(c)) << endl;

    return 0;
    

代码解析

  1. 声明了一个Class类,并为Class类添加了3种构造函数。分别是:普通构造函数,拷贝构造函数和移动构造函数。
    拷贝构造函数,使用类对象的左值引用作为参数。
    移动构造函数,使用类对象的右值引用作为参数。
    普通构造函数,无参数。
  2. main函数中:
    2.1 Class a, 定义一个 a 对象,调用普通构造函数。
    2.2 Class b(a) 定义对象 b, 此时调用拷贝构造函数。
    2.3 Class &&c 定义对象 c, 此时先调用普通构造函数,创建临时对象,然后再调用移动构造函数,然后析构掉临时对象。
    2.4 析构c
    2.5 析构b
    2.6 析构a

Makefile

all:
    g++ -std=c++11 -fno-elide-constructors test.cpp -o test    

注意: 编译参数中加入:-std=c++11 -fno-elide-constructors

运行截图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

化妖成魔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值