如何使用包含唯一指针的成员变量创建可复制赋值的 class?
How can I make a class, with a member variable that contains unique pointers, copy assignable?
我有一个 class (PlayerCharacter),其中包含一张地图,而地图又包含唯一的指针。我还有另一个 class(派对),其中应该包含第一个 class 的多个实例。当我尝试为采用 PlayerCharacter 实例并将其复制到向量的第一个元素的 Party 实现构造函数时,我遇到了多个错误(使用 g++),这些错误太长而无法放入这个问题中。
我认为错误的产生是因为 BaseCharacter 和 PlayerCharacter 由于唯一指针而不可复制分配。如何为此 class 实现完整的复制构造函数?
MCVE低于
#include <map>
#include <utility>
#include <memory>
#include <vector>
enum class EQUIPMENT_SLOT {
HEAD
};
class WeaponItem {};
class ClothingItem{};
class BaseCharacter {
private:
std::map<EQUIPMENT_SLOT, std::pair<std::unique_ptr<WeaponItem>, std::unique_ptr<ClothingItem>>> _equipment;
};
class PlayerCharacter : public BaseCharacter {};
class Party {
private:
std::vector<PlayerCharacter> _party_members;
public:
// Addition of this constructor causes errors to appear
Party(PlayerCharacter player){
this->_party_members.clear();
this->_party_members.push_back(player); // This line is the problem - commenting it out compiles fine
};
};
int main(){
return 0;
}
如果您在 unique_ptr 中持有物品,这通常意味着您不想复制它们。如果你想复制它们,你必须提供这样做的机制:
struct my_thing {
};
using my_thing_ptr = std::unique_ptr<my_thing>;
struct my_container
{
my_container() {}
// the presence of _thing will implicitly delete copy constructor
// and copy operator so we need to provide them.
// Since we're defining copy operators, these will disable
// the automatic move operators so we need to define them too!
my_container(const my_container& rhs)
: _thing { rhs._thing ? new thing { *(rhs._thing) } : nullptr }
{}
my_container(my_container&& rhs) = default;
my_container& operator=(const my_container&& rhs) {
auto tmp = rhs;
swap(tmp, *this);
return *this;
}
my_container& operator=(my_container&& rhs) = default;
// and an implementation of swap for good measure
void swap(my_container& other) noexcept {
using std::swap;
swap(_thing, other._thing);
}
my_thing_ptr _thing;
};
我有一个 class (PlayerCharacter),其中包含一张地图,而地图又包含唯一的指针。我还有另一个 class(派对),其中应该包含第一个 class 的多个实例。当我尝试为采用 PlayerCharacter 实例并将其复制到向量的第一个元素的 Party 实现构造函数时,我遇到了多个错误(使用 g++),这些错误太长而无法放入这个问题中。
我认为错误的产生是因为 BaseCharacter 和 PlayerCharacter 由于唯一指针而不可复制分配。如何为此 class 实现完整的复制构造函数?
MCVE低于
#include <map>
#include <utility>
#include <memory>
#include <vector>
enum class EQUIPMENT_SLOT {
HEAD
};
class WeaponItem {};
class ClothingItem{};
class BaseCharacter {
private:
std::map<EQUIPMENT_SLOT, std::pair<std::unique_ptr<WeaponItem>, std::unique_ptr<ClothingItem>>> _equipment;
};
class PlayerCharacter : public BaseCharacter {};
class Party {
private:
std::vector<PlayerCharacter> _party_members;
public:
// Addition of this constructor causes errors to appear
Party(PlayerCharacter player){
this->_party_members.clear();
this->_party_members.push_back(player); // This line is the problem - commenting it out compiles fine
};
};
int main(){
return 0;
}
如果您在 unique_ptr 中持有物品,这通常意味着您不想复制它们。如果你想复制它们,你必须提供这样做的机制:
struct my_thing {
};
using my_thing_ptr = std::unique_ptr<my_thing>;
struct my_container
{
my_container() {}
// the presence of _thing will implicitly delete copy constructor
// and copy operator so we need to provide them.
// Since we're defining copy operators, these will disable
// the automatic move operators so we need to define them too!
my_container(const my_container& rhs)
: _thing { rhs._thing ? new thing { *(rhs._thing) } : nullptr }
{}
my_container(my_container&& rhs) = default;
my_container& operator=(const my_container&& rhs) {
auto tmp = rhs;
swap(tmp, *this);
return *this;
}
my_container& operator=(my_container&& rhs) = default;
// and an implementation of swap for good measure
void swap(my_container& other) noexcept {
using std::swap;
swap(_thing, other._thing);
}
my_thing_ptr _thing;
};