【发布时间】:2020-07-09 22:59:34
【问题描述】:
我正在尝试编写 Matrix 类。我使用 unique_ptr 作为存储指针数组。但是这个 unique_ptr 是 private 类的成员。我想将移动构造函数添加到我的 Matrix 类。但是我需要 getter 函数用于私有 unique_ptr 但是当我将 unique_ptr 作为 const 变量返回时,我不能使用 std::move 但是当我返回没有 const 参考编译器的 getter 函数时'不允许这样。
矩阵.hpp
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_()
{
}
Test.cpp
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;
【问题讨论】:
-
为什么你需要一个 getter 来表示内部数据,并且 谁 在 getter 调用之后应该是这个数据的所有者?它是否仍应归
Matrix_所有,还是应让 getter 调用者成为数据的唯一所有者? -
所有者必须相同 我需要 getter 来获取参数的数据成员,例如 'Matrix_<_t>::Matrix_(Matrix_&& m)' 我需要获取 _m 的数据变量
-
这是一个member函数。成员函数可以访问类的所有私有成员(无论要访问同一个对象还是不同对象,类都是一样的)。如果您需要从类的 outside 访问它(也就是不是
Matrix_成员的任何函数),您只需要一个 getter -
谢谢。我不知道。但我仍然想知道为什么我不能使用 getData() 我返回私有成员作为参考
-
不要使用以下划线开头后跟大写字母的标识符;这些是为实现而保留的。不要忘记同时进行移动分配
noexcept。
标签: c++ smart-pointers move-semantics move-constructor