- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一整天都在做这件事,所以我希望我不会忘记任何重要的细节,但现在就开始吧。我最初的目标是拥有一个播放器工厂来封装如何创建播放器的逻辑。
这是它的样子:
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/
假设我有一个简单的模型: class Contact(models.Model): owner = models.ForeignKey(User, editable=False) fi
我在docker容器中的轨道上运行ruby。如果生成类似docker-compose run rails rails g controller posts index show的文件,则文件所有者为r
这个问题在这里已经有了答案: Get Component's Parent Form (10 个答案) 关闭 3 年前。 如何从嵌入到表单中的自定义 Winforms 组件获取父/所有者控件? 我有
我这里有密码 @interface FFRightSidebarController () @property (nonatomic, strong) FFActivitiesController *
假设您有这些表:RestaurantChains、Restaurants、MenuItems - 它们之间存在明显的关系。现在,您有表 Comments 和 Ratings,它们存储客户对链式店、餐馆
我有以下 View 层次结构, ... ... .... 单个订单的更新通过推送器进行。 我是 React 新手,想知道以下哪一个是更
所以我有一个位于另一个用户控件内部的用户控件。我们称它们为 ParentUC 和 ChildUC。我需要从 ChildUC 获取 ParentUC。 我知道获取窗口所有者的方法是Window.GetW
我们创建了 Telegram Bot ,它有许多困难的操作。机器人是由一位开发人员使用他的电话号码创建的。是否可以更改机器人的所有者或添加其他用户作为机器人的管理员? 最佳答案 最近出现了将机器人转给
默认情况下,对象(表、存储过程等)是使用 dbo 所有者/架构设置的(我认为 ms sql 2000 称其为所有者,而 ms sql 2005 称其为架构) 所有者/架构实际上是数据库中的角色或用户。
在 Mortar 中,如果 ActionBar 根据显示的屏幕发生变化,我很好奇人们如何处理它。例如,假设您想要在显示特定屏幕时更改 ActionBar 标题或操作。 注入(inject) Activ
我正在使用 Stripe.js 和 Stripe Elements 开发购物车结帐页面,而不是使用 Stripe 的结帐小部件。 在结帐页面上,使用了所有 4 个可用元素(卡片、邮政编码、expire
我尝试将本地仓库推送到 github 并收到这样的消息: The remote end hung up unexpectedly. ERROR: Permission to [repo_name] d
我是 WHM、cPanel 和 CentOS 的新手。 我安装 WHM 然后为域创建一个帐户 app.example.com和用户 peter我将域名指向正确的 IP 地址,但是当我运行我的网站 ap
我已经在 GitHub 上提交了一个项目的问题,该项目不是我的,我也不是贡献者,但我找不到标记我的问题的方法。有没有办法让我给它贴上标签,或者这只适用于贡献者? 最佳答案 它仅适用于贡献者。 这样,您
我到处都找过了,但一直找不到我要找的东西。我知道我需要什么,但无法将 2 和 2 放在一起。 我需要允许用户创建群组并允许其他用户加入群组。 任何用户都可以创建群组。 任何用户都可以发送加入另一个群组
我到处都找过了,但一直找不到我要找的东西。我知道我需要什么,但无法将 2 和 2 放在一起。 我需要允许用户创建群组并允许其他用户加入群组。 任何用户都可以创建群组。 任何用户都可以发送加入另一个群组
这是我的代码: class SpecialMeanings{ String prop1 = "prop1" def closure = { String prop1 = "inner_
我一直在使用 java OWNER 进行基于属性的配置。 我创建了一个静态方法 public static final ApplicationConfiguration config = Config
我正在运行 OpenSSH sftp-server(Linux、Raspbian),FileZilla 用作客户端。我遇到的问题是用户可以删除服务器上的任何文件,而不管文件掩码或所有者/组: 登录的用
这是一个简单的问题,我无法通过谷歌搜索和查看 github documents 找到答案。 . 如果有人对 github 中已关闭的问题发表新评论,是否会通知 Repo 所有者? 最佳答案 如果向已关
我是一名优秀的程序员,十分优秀!