gpt4 book ai didi

移动所有者时,对所有者的 C++ 复合引用已损坏

转载 作者:行者123 更新时间:2023-11-30 00:56:51 24 4
gpt4 key购买 nike

我一整天都在做这件事,所以我希望我不会忘记任何重要的细节,但现在就开始吧。我最初的目标是拥有一个播放器工厂来封装如何创建播放器的逻辑。

这是它的样子:

Player PlayerFactory::CreatePlayer() {

Player constructee(id_generator++);

// c++1x move semantics says this isn't a copy!
return constructee;
}

在播放器中,有一个复合的 PlayerInputComponent 成员变量。这个 PlayerInputComponent 对象封装了处理玩家输入的逻辑,为此它需要一个指向实际玩家本身的引用/指针。很简单,它采用对播放器的引用,因为它是其构造函数的唯一参数。

构建播放器对象时,它的初始化列表将对自身的引用传递给 PlayerInputComponent 对象。这是它的样子:

Player::Player(const unsigned int id) 
: id(id),
input_component(*this)
{
...
}

我知道在初始化列表中取消引用它通常不是一个好主意,但我只是用它来设置 PlayerInputComponent 对象中的引用。这是构造函数:

PlayerInputComponent::PlayerInputComponent(Player &player) 
: player_entity(player) { // set the reference, it is never used within the constructor body
}

无论出于何种原因,当播放器工厂返回其创建的播放器的本地拷贝时,引用会出现乱码。我的意图是将在堆栈上创建的玩家实例移动并分配给被调用者。这看起来像下面这样:

auto player = player_factory.CreatePlayer();

代码执行后,播放器的复合 PlayerInputComponent 对象对其播放器的引用被破坏。我知道当工厂将本地播放器对象返回给被调用者时会调用播放器的移动构造函数,但是播放器在 PlayerInputComponent 中的引用会发生什么情况?有没有更好的方法来解决这个问题?我喜欢在这种情况下使用引用与指针的语义,尽管我确实使用指针进行了尝试并得到了相同的结果。

谁能向我解释当播放器对象移出 CreatePlayer() 成员函数并分配给“自动播放器”时,PlayerInputComponent 对播放器的引用发生了什么?

为了完整起见,这里是对象的声明:

class PlayerInputComponent {

private:
Player &player_entity;
void HandleKeyboardInput();
public:
//PlayerInputComponent(PlayerInputComponent &&other);
//PlayerInputComponent(const PlayerInputComponent &other);
PlayerInputComponent(Player &player);
void Update();
};

这是播放器:

class Player : public Entity{
friend class PlayerFactory;
private:
const unsigned int id;

public:
Player(const unsigned int id);

// components
PlayerInputComponent input_component;
};

我在下面重建了一个小示例,它显示了编译/演示问题的确切行为。谢谢!

class B;

class A {
public:
A(B& bb) : b(bb) { }

private:
B &b;
};

class B {
public:
B() : a(*this) { othercrap = othercrap2 = 0; }
int othercrap;
int othercrap2;
private:
A a;
};

class BFactory {
public:
B CreateB() {
B temp;
return temp;
};
};

B start() {
BFactory fac;
auto bi = fac.CreateB();

return bi;
}

int main(int argc, char *argv[]) {
auto i = start();
}

最佳答案

移动会使引用无效。 (引用是指向存储位置,而不是对象)

请记住,移动会创建一个新对象(因此得名 move constructor)。内容的所有权已转移,但对象实际上并未移动到内存中的新位置。构造新对象后销毁。

您应该定义 Player 的复制和移动构造函数以正确绑定(bind)新对象中的引用。

关于移动所有者时,对所有者的 C++ 复合引用已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9355744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com