C++移动构造函数,移动赋值和编译选项-fno-elide-constructors

该文展示了C++中类Array2D的构造函数、拷贝构造函数、移动构造函数以及赋值运算符的实现和使用。在不同编译选项下,如-fno-elide-constructors,其行为有所差异,涉及到对象创建、拷贝、移动以及内存管理的过程。通过示例代码解释了这些函数在实际操作中的作用,尤其是在优化上下文中。
摘要由CSDN通过智能技术生成

示例代码:

#include <iostream>

using namespace std;
class Array2D {
private:
    int m,n;  // m rows, n cols
    int *array;
public:
    Array2D(int _m, int _n):m(_m), n(_n) {
        cout << "ctor" << endl;
        array = new int[m*n];
    }

    Array2D(const Array2D &a) {
        cout << "copy ctor" << endl;
        int j,k;
        m = a.m;
        n = a.n;
        array = new int[m*n];
        for(int i = 0; i < m*n; ++i) {
            k = i % a.n;
            j = (i - k) /n;
            array[i] = a(j, k);
        }
    }

    Array2D(Array2D &&a) noexcept {
        cout << "move ctor" << endl;
        m = a.m;
        n = a.n;
        array = a.array;
        a.array = nullptr;
    }


    ~Array2D() {
        delete[] array;
        cout << "dtor" << endl;
    }
    int& operator()(int x, int y) {
        return array[x*n+y];
    }

    const int& operator()(int x, int y) const {
        return array[x*n+y];
    }

    Array2D& operator=(const Array2D &a) {
        cout << "call operator=" << endl;
        if (this->array != a.array) {
            int j,k;
            this->m = a.m;
            this->n = a.n;
            delete[] this->array;
            this->array = new int[m*n];
            for(int i = 0; i < m*n; ++i) {
                k = i % a.n;
                j = (i - k) /n;
                this->array[i] = a(j, k);  //calling: const int& operator()(int x, int y) const
            }
        }
        return *this;
    }

    Array2D& operator=(Array2D &&a) {
        cout << "call move operator=" << endl;
        m = a.m;
        n = a.n;
        array = a.array;
        a.array = nullptr;
        return *this;
    }

};


Array2D getArray2D() {
    cout << "in getArray2D()" << endl;
    Array2D a2d(1000,1000);
    a2d(100,100) = 100;
    a2d(200,200) = 200;
    a2d(900,900) = 900;
    return a2d;
}

(1)main中的测试代码:

Array2D big1 = getArray2D();
cout << big1(100,100) << " " << big1(200,200) << " " << big1(900,900) << endl;

C++98

C++11 & C++14

C++17 & C++20

With

-fno-elide-constructors

in getArray2D()

ctor

copy ctor

dtor

copy ctor

dtor

100 200 900

dtor

in getArray2D()

ctor

move ctor

dtor

move ctor

dtor

100 200 900

dtor

in getArray2D()

ctor

move ctor

dtor

100 200 900

dtor

Without

-fno-elide-constructors

in getArray2D()

ctor

100 200 900

dtor

(2)main中的测试代码:

Array2D big3(1,1);
big3  = getArray2D();
cout << big3(100,100) << " " << big3(200,200) << " " << big3(900,900) << endl;

C++98

C++11 & C++14 & C++17 & C++20

With

-fno-elide-constructors

ctor

in getArray2D()

ctor

copy ctor

dtor

call operator=

dtor

100 200 900

dtor

ctor

in getArray2D()

ctor

move ctor

dtor

call move operator=

dtor

100 200 900

dtor

Without

-fno-elide-constructors

tor

in getArray2D()

ctor

call operator=

dtor

100 200 900

dtor

ctor

in getArray2D()

ctor

call move operator=

dtor

100 200 900

dtor

说明:使用C++98标准编译时,需要注释掉移动构造函数和移动赋值函数

-fno-elide-constructors

The C++ standard allows an implementation to omit creating a temporary that is only used to initialize another object of the same type. Specifying this option disables that optimization, and forces G++ to call the copy constructor in all cases.

 默认情况下,不带-fno-elide-constructors选项,编译器会进行良好的优化。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值