我正在尝试编写Matrix类。我正在使用unique_ptr作为存储指针数组。但是,这个unique_ptr是类的私有成员。我想将move构造函数添加到Matrix类中。但是我需要私有的unique_ptr的getter函数,但是当我将unique_ptr作为const变量返回时,我不能使用std :: move,但是当我在没有const引用编译器的情况下返回getter函数时,则不允许这样做。
矩阵
template <typename _T>
class Matrix_
{
private:
size_t rows_;
size_t cols_;
size_t size_;
std::unique_ptr<_T[]> data_;
public:
Matrix_();
Matrix_(const Matrix_& _m); //Copy constructor
Matrix_(Matrix_&& _m) noexcept; //Move constructor
Matrix_(const size_t& rows);
Matrix_(const size_t& rows,const size_t& cols);
Matrix_(const size_t& rows,const size_t& cols,const _T& val);
//Operators
Matrix_& operator=(const Matrix_& _m); //Copy assignment
Matrix_& operator=(Matrix_&& _m) noexcept; //Move assignment
//
//Methods
size_t getRows()const;
size_t getCols()const;
size_t getSize()const;
const std::unique_ptr<_T[]>& getData()const;
void copyData(const std::unique_ptr<_T[]>& _data);
void fill(const _T& val);
//
~Matrix_();
};
template <typename _T>
Matrix_<_T>::Matrix_():rows_(1),cols_(1),size_(rows_*cols_),data_(std::make_unique<_T[]>(size_))
{
}
template <typename _T>
Matrix_<_T>::Matrix_(const Matrix_& _m):rows_(_m.getRows()),cols_(_m.getCols()),size_(rows_*cols_),data_(std::make_unique<_T[]>(size_))
{
}
template <typename _T>
Matrix_<_T>::Matrix_(Matrix_&& _m)noexcept:rows_(_m.getRows()),cols_(_m.getCols()),size_(rows_*cols_),data_(std::move(_m.getData()))
{
}
template <typename _T>
Matrix_<_T>::Matrix_(const size_t& rows):rows_(rows),cols_(1),size_(rows_*cols_),data_(std::make_unique<_T[]>(size_))
{
}
template <typename _T>
Matrix_<_T>::Matrix_(const size_t& rows,const size_t& cols):rows_(rows),cols_(cols),size_(rows_*cols_),data_(std::make_unique<_T[]>(size_))
{
}
template <typename _T>
Matrix_<_T>::Matrix_(const size_t& rows,const size_t& cols,const _T& val):rows_(rows),cols_(cols),size_(rows_*cols_),data_(std::make_unique<_T[]>(size_))
{
fill(val);
}
//Operators
template <typename _T>
Matrix_<_T>& Matrix_<_T>::operator=(const Matrix_& _m)
{
rows_ = _m.rows_;
cols_ = _m.cols_;
size_ = rows_*cols_;
data_ = std::make_unique<_T[]>(size_);
copyData(_m.getData());
return *this;
}
template <typename _T>
Matrix_<_T>& Matrix_<_T>::operator=(Matrix_&& _m)noexcept{
rows_ = _m.rows_;
cols_ = _m.cols_;
size_ = rows_*cols_;
data_ = std::move(_m.getData());
return *this;
}
//
//Methods
template <typename _T>size_t Matrix_<_T>::getRows()const{return rows_;}
template <typename _T>size_t Matrix_<_T>::getCols()const{return cols_;}
template <typename _T>size_t Matrix_<_T>::getSize()const{return size_;}
template <typename _T>const std::unique_ptr<_T[]>& Matrix_<_T>::getData()const{return data_;}
template <typename _T>void Matrix_<_T>::copyData(const std::unique_ptr<_T[]>& _data){
for(uint i=0;i<size_;i++){data_[i]=_data[i];}
}
template <typename _T>void Matrix_<_T>::fill(const _T& val){
for(uint i=0;i<size_;i++){data_[i]=val;}
}
//
template <typename _T>
Matrix_<_T>::~Matrix_()
{
}
测试文件
int main()
{
Matrix_<double> a(10,10,5.0);
Matrix_<double> b(10,10);
b = std::move(a);
std::cout<<b.getData()[0]<<std::endl;
return 0;
}
错误
error: use of deleted function ‘std::unique_ptr<_Tp [], _Dp>& std::unique_ptr<_Tp [], _Dp>::operator=(const std::unique_ptr<_Tp [], _Dp>&) [with _Tp = double; _Dp = std::default_delete<double []>]’
data_ = std::move(_m.getData());
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
from /usr/include/c++/7/locale:43,
from /usr/include/c++/7/iomanip:43,
from /home/cemo/YZlib/Examples/Test.cpp:3:
/usr/include/c++/7/bits/unique_ptr.h:654:19: note: declared here
unique_ptr& operator=(const unique_ptr&) = delete;
不要在移动构造函数或移动分配中使用吸气剂。只需直接使用该成员。我可能会对所有成员都这样做,以使他们很明显地相互匹配。
template <typename T>
Matrix<T>::Matrix(Matrix &&m) noexcept :
rows_(m.rows_), cols_(m.cols_), size_(m.size_), data_(std::move(m.data_))
{
// changes needed to m?
}
template <typename T>
Matrix<T>& Matrix<T>::operator=(Matrix &&m) noexcept
{
rows_ = m.rows_;
cols_ = m.cols_;
size_ = m.size_;
data_ = std::move(m.data_);
// changes needed to m?
return *this;
}
类始终有权访问其自己的成员(无论通过哪个对象访问它们)。
m
由于它data_
是一个空指针,因此请务必考虑现在该怎么办。它的rows_
,cols_
和size_
应该设置为零,还是不重要?通常,无论您决定对班级意味着什么,移出的对象都应保持“有效但未指定”状态。至少,调用析构函数应该是有效的,并将其分配给其他对象应该可以正常工作;这已经是事实了。
由于这里不需要使用getter,因此,这是一个单独的问题,即公共接口是否应该完全包含getter,或者是否应该具有更便捷的return类型(如justconst T*
或)T*
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句