小编Eri*_*man的帖子

为什么我的代码中没有调用move构造函数?

#include <iostream>
#include<algorithm>

template<class T>
class Matrix {
    std::pair<unsigned int,unsigned int> dim;
    T* elem;
public:
    Matrix(unsigned int d1, unsigned int d2) :
        dim{std::make_pair(d1,d2)}, elem{new T[d1*d2]} { }

    unsigned int size() const { return (dim.first)*(dim.second); }

    Matrix(Matrix&& a){
        std::cout<<"move constructor";
        elem = a.elem;
        a.elem =nullptr;
        dim.first = a.dim.first+7;
        dim.second = a.dim.second;
        a.dim.first=0;
        a.dim.second=0;
    }

    Matrix& operator=(Matrix&& a){
        std::cout<<"move operator=";
        elem = a.elem;
        a.elem =nullptr;
        dim.first = a.dim.first;
        dim.second = a.dim.second;
        a.dim.first=0;
        a.dim.second=0;
        return *this;
    }

    ~Matrix() { delete[] elem; }
}; …
Run Code Online (Sandbox Code Playgroud)

c++ move

1
推荐指数
1
解决办法
99
查看次数

标签 统计

c++ ×1

move ×1