I'm creating my own custom Filter class for use in boost::filtered_graph. The WeightMap concept must have a default constructor, copy constructor, and assignment operator.
I've created the class below, which has a std::shared_ptr private member. My question is how I'm supposed to write the assignment operator. The copy constructor wasn't a problem, but the assignment operator doesn't work.
class BFDMFilter
{
private:
const BGraph* m_battlemap;
const std::shared_ptr<MoveAbility> m_mv_ab;
public:
BFDMFilter() : m_battlemap(nullptr), m_mv_ab() { }
BFDMFilter(const BGraph* bmap, std::shared_ptr<MoveAbility> mv) : m_battlemap(bmap), m_mv_ab(mv) { }
BFDMFilter(const BFDMFilter& filter) : m_battlemap(filter.m_battlemap), m_mv_ab(filter.m_mv_ab) { }
BFDMFilter& operator=(const BFDMFilter& filter)
{
if(this != &filter)
{
m_battlemap = filter.m_battlemap;
m_mv_ab = filter.m_mv_ab;
}
return *this;
}
bool operator()(const Edge& edge) const
{
Tile::TileEdge path = (*m_battlemap)[edge];
return m_mv_ab->CanMove(path.TerrainType()) > 0.0;
}
bool operator()(const Vertex& vertex) const
{
Tile tile = (*m_battlemap)[vertex];
return m_mv_ab->CanMove(tile.TerrainType()) > 0.0;
}
};
Which then gives me a compile error:
error: passing ‘const std::shared_ptr<momme::battle::MoveAbility>’ as ‘this’ argument of ‘std::shared_ptr<_Tp>& std::shared_ptr<_Tp>::operator=(std::shared_ptr<_Tp>&&) [with _Tp = momme::battle::MoveAbility, std::shared_ptr<_Tp> = std::shared_ptr<momme::battle::MoveAbility>]’ discards qualifiers [-fpermissive]
I understand why; the assignment operator modifies the reference count of the shared_ptr when it does the assignment, so that it can keep track of how many open references there are. But then, how do I write the assignment operator? std::weak_ptr has the same behavior, and if I make the reference non-const, then the boost library complains that the function is deleted.
std::shared_ptr<const MoveAbility> m_mv_abinstead? Do you want thestd::shared_ptritself const or the object it's pointing at to be const?constdata member. This isn't specific toshared_ptr.