[英]In class assign operator with std::unique_ptr,
I am having a problem with the operator=
in my BitMap
class.我的
BitMap
class 中的operator=
有问题。 I was having some memory leaks so I decided to change from a raw pointer to a std::unique_ptr
.我有一些 memory 泄漏,所以我决定从原始指针更改为
std::unique_ptr
。 When I did that, I noticed that whenever I use my BitMap
assignment operator, I get an error:当我这样做时,我注意到每当我使用我的
BitMap
赋值运算符时,我都会收到一个错误:
function "sp::BitMap::BitMap(const sp::BitMap &)" (declared implicitly) cannot be referenced -- it is a deleted function
function "sp::BitMap::BitMap(const sp::BitMap &)" (declared implicitly) cannot be referenced -- it is a deleted function
.h file .h 文件
#include "color.h"
#include "spmath.h"
#include <memory>
namespace sp
{
//Bitmap: saves pixel position in the image
class BitMap
{
public:
BitMap();
BitMap(const vector2i& pos, const vector2i& size);
~BitMap() { }
BitMap& operator=(BitMap bm);
void marge(BitMap& bm);
void marge(BitMap* bm, int obj_count);
void calculateNewRect(const BitMap& bm, sp::vector2i* pos, sp::vector2i* size);
void calculateNewRect(const BitMap* bm, int obj_count, sp::vector2i* pos, sp::vector2i* size);
void margeToBitMap(BitMap& target, BitMap& bm);
public:
//bool* m_pixelMap;
std::unique_ptr<bool[]> m_pixelMap;
vector2i m_startPos;
vector2i m_size;
};
//--------------------------
}
.cpp file .cpp 文件
//-----------------------------------------------------
sp::BitMap& sp::BitMap::operator=(BitMap bm)
{
//if(this->m_pixelMap != nullptr)
// this->clear(); //Delete last pixel_map
this->m_startPos = bm.m_startPos; //Copy position
this->m_size = bm.m_size; //Copy size
this->m_pixelMap.reset(bm.m_pixelMap.release()); //Copy ptr to pixel_map
//bm.m_pixelMap = nullptr; //Delete pointer to pixel map
return *this;
}
//-----------------------------------------------------
//-----------------------------------------------------
void sp::BitMap::marge(BitMap& bm) //Margeing to one object
{
//Calcuating new bitmap size
vector2i new_pos = this->m_startPos;
vector2i new_sum_size = this->m_size + new_pos;
calculateNewRect(bm, &new_pos, &new_sum_size);
vector2i new_size = new_sum_size - new_pos;
BitMap marged(new_pos, new_size);
//------------------------------
//Mergeing
margeToBitMap(marged, *this);
margeToBitMap(marged, bm);
//------------------------------
*this = marged; //ERROR
}
//-----------------------------------------------------
You take the parameter of operator=
by value, so this requires marged
be copied into the argument as written.您按值获取
operator=
的参数,因此这需要将marged
复制到所写的参数中。 However, the implicit copy constructor is ill-formed since m_pixelMap
cannot be copied.但是,隐式复制构造函数格式不正确,因为无法复制
m_pixelMap
。
Instead, consider move-assigning since the local is going to be destroyed anyway:相反,考虑移动分配,因为无论如何本地都会被破坏:
*this = std::move(marged);
This move-constructs the argument.这一步构建了论点。 The implicit move constructor is well-formed (assuming
vector2i
is movable).隐式移动构造函数格式正确(假设
vector2i
是可移动的)。
As an alternative approach, consider replacing one or more of the data members with an std::vector<std::byte>
.作为一种替代方法,考虑用
std::vector<std::byte>
替换一个或多个数据成员。 This will make copying, moving, and destruction all automatic.这将使复制、移动和销毁全部自动化。
If bit-packing of the flags is acceptable in your case (it's hard to tell since we don't see m_pixelMap
actually used anywhere) then you can use the specialization std::vector<bool>
.如果在您的情况下可以接受标志的位打包(很难说,因为我们没有看到
m_pixelMap
在任何地方实际使用),那么您可以使用专业化std::vector<bool>
。
After some testing with std::vector<bool>
I found out that this solution consumes too much CPU and memory dropping average FPS from 80 to 6. I couldn't also get std::move()
to work for my purposes.在使用
std::vector<bool>
进行一些测试后,我发现这个解决方案消耗了太多 CPU,并且 memory 将平均 FPS 从 80 降至 6。我也无法让std::move()
为我的目的工作。 So I came back to my original solution and handled the memory leaks.所以我回到我原来的解决方案并处理了 memory 泄漏。 Thanks to @cdhowie for pointing out the
unique_ptr
copy problem I will remember that for the future.感谢@cdhowie 指出
unique_ptr
复制问题,我会记住这一点。
.h file .h 文件
namespace sp
{
//Bitmap: saves pixel position in the image
class BitMap
{
public:
BitMap();
BitMap(const vector2i& pos, const vector2i& size);
~BitMap();
BitMap& operator=(BitMap bm);
void clear();
void marge(BitMap& bm);
void marge(BitMap* bm, int obj_count);
void calculateNewRect(const BitMap& bm, sp::vector2i* pos, sp::vector2i* size);
void calculateNewRect(const BitMap* bm, int obj_count, sp::vector2i* pos, sp::vector2i* size);
void margeToBitMap(BitMap& target, BitMap& bm);
public:
bool* m_pixelPosMap;
vector2i m_startPos;
vector2i m_size;
};
//--------------------------
}
.cpp file .cpp 文件
sp::BitMap::BitMap() :
m_startPos(sp::vector2i(0, 0)) , m_size(sp::vector2i(0, 0)), m_pixelPosMap(nullptr)
{
}
sp::BitMap::BitMap(const vector2i& pos, const vector2i& size) :
m_startPos(pos), m_size(size)
{
m_size.x;
m_size.y;
int full_size = m_size.x * m_size.y;
m_pixelPosMap = new bool[full-size];
memset(m_pixelPosMap, 0, full_size); //Clear pixelMap
}
sp::BitMap::~BitMap()
{
}
//-----------------------------------------------------
//-----------------------------------------------------
sp::BitMap& sp::BitMap::operator=(BitMap bm)
{
this->clear(); //Delete last pixel_map
this->m_startPos = bm.m_startPos; //Copy position
this->m_size = bm.m_size; //Copy size
this->m_pixelPosMap = bm.m_pixelPosMap; //Copy ptr to pixel_map
bm.m_pixelPosMap = nullptr; //Delete pointer to pixel map
return *this;
}
//-----------------------------------------------------
void sp::BitMap::clear()
{
if(m_pixelPosMap != nullptr)
delete[] m_pixelPosMap;
}
//-----------------------------------------------------
void sp::BitMap::marge(BitMap& bm) //Margeing to one object
{
//Calcuating new bitmap size
vector2i new_pos = this->m_startPos;
vector2i new_sum_size = this->m_size + new_pos;
calculateNewRect(bm, &new_pos, &new_sum_size);
vector2i new_size = new_sum_size - new_pos;
BitMap marged(new_pos, new_size);
//------------------------------
//Margeing
margeToBitMap(marged, *this);
margeToBitMap(marged, bm);
//------------------------------
*this = marged;
}
//-----------------------------------------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.