初始化成员 unique_ptr 为空
initialize a member unique_ptr to empty
在我的程序中,我有一堆自定义 class 位置的对象。 Position声明如下:
class Position {
public:
Position(int x, int y);
~Position();
Actor *getActor() { return actor.get(); };
void setActor(Actor *actor) { actor = std::move(actor); };
Actor *clearActor() { return actor.release(); };
int getX() { return x; };
int getY() { return y; };
private:
int x, y;
std::unique_ptr<Actor> actor;
};
我还有一个class叫Actor。不是每个 Position 都会有一个 Actor,所以大多数时候 Position 对象的 unique_ptr "actor" 应该是空的(我使用 unique_ptrs 来自动清理任何 Actor在运行时与 Position 相关联)。
Position构造函数如下:
Position::Position(int x, int y)
{
this->x = x;
this->y = y;
actor.reset(nullptr);
}
但是,我知道这没有正确地将存储的指针设置为 nullptr,因为当我尝试在 Position::getActor() 中调用 actor.get() 时,我收到如下错误:
____.exe 中 0x01096486 处的第一次机会异常:0xC0000005:访问冲突读取位置 0x00000008。
有没有办法将成员 unique_ptr 初始化为 nullptr?我知道我可以通过向 Actor class 添加一个变量来解决这个问题,该变量定义 Actor 是否处于活动状态,将 unique_ptr 设置为新的非活动 Actor,并忽略所有非活动 Actor,但我如果可能的话,我宁愿避免这种情况。
谢谢!
编辑:我在调用 getActor 的地方添加了代码:
bool Grid::addActor(Actor *actor, int x, int y)
{
Position *destination = at(x, y);
if (!destination->getActor()) {
destination->setActor(actor);
actor->setPosition(x, y);
actor->setGrid(this);
return true;
}
else {
inactive_actors.emplace_back(actor);
return false;
}
}
您不需要将 std::unique 指针初始化为空。只需将其保留为构造函数中的默认空值,并且仅将其重置为指向非空指针。
你的错误在这里:
void setActor(Actor *actor) { actor = std::move(actor); };
您正在将 std::move
的结果分配给 参数 actor
。你可能想 reset
成员变量 actor
与参数 actor
:
void setActor(Actor *actor) { this->actor.reset(actor); };
附带说明一下,您只需将构造函数更改为:
Position::Position(int x, int y)
: x(x), y(y)
{
}
这将使用参数初始化成员 x
和 y
,并将 std::unique_ptr<Actor> actor
默认初始化为 null。
在我的程序中,我有一堆自定义 class 位置的对象。 Position声明如下:
class Position {
public:
Position(int x, int y);
~Position();
Actor *getActor() { return actor.get(); };
void setActor(Actor *actor) { actor = std::move(actor); };
Actor *clearActor() { return actor.release(); };
int getX() { return x; };
int getY() { return y; };
private:
int x, y;
std::unique_ptr<Actor> actor;
};
我还有一个class叫Actor。不是每个 Position 都会有一个 Actor,所以大多数时候 Position 对象的 unique_ptr "actor" 应该是空的(我使用 unique_ptrs 来自动清理任何 Actor在运行时与 Position 相关联)。
Position构造函数如下:
Position::Position(int x, int y)
{
this->x = x;
this->y = y;
actor.reset(nullptr);
}
但是,我知道这没有正确地将存储的指针设置为 nullptr,因为当我尝试在 Position::getActor() 中调用 actor.get() 时,我收到如下错误:
____.exe 中 0x01096486 处的第一次机会异常:0xC0000005:访问冲突读取位置 0x00000008。
有没有办法将成员 unique_ptr 初始化为 nullptr?我知道我可以通过向 Actor class 添加一个变量来解决这个问题,该变量定义 Actor 是否处于活动状态,将 unique_ptr 设置为新的非活动 Actor,并忽略所有非活动 Actor,但我如果可能的话,我宁愿避免这种情况。
谢谢!
编辑:我在调用 getActor 的地方添加了代码:
bool Grid::addActor(Actor *actor, int x, int y)
{
Position *destination = at(x, y);
if (!destination->getActor()) {
destination->setActor(actor);
actor->setPosition(x, y);
actor->setGrid(this);
return true;
}
else {
inactive_actors.emplace_back(actor);
return false;
}
}
您不需要将 std::unique 指针初始化为空。只需将其保留为构造函数中的默认空值,并且仅将其重置为指向非空指针。
你的错误在这里:
void setActor(Actor *actor) { actor = std::move(actor); };
您正在将 std::move
的结果分配给 参数 actor
。你可能想 reset
成员变量 actor
与参数 actor
:
void setActor(Actor *actor) { this->actor.reset(actor); };
附带说明一下,您只需将构造函数更改为:
Position::Position(int x, int y)
: x(x), y(y)
{
}
这将使用参数初始化成员 x
和 y
,并将 std::unique_ptr<Actor> actor
默认初始化为 null。