我如何推堆栈unqiue_ptr不做一个副本
How do I push to stack with unqiue_ptr without making a copy?
最初我做了一个抽象类型为GameState*
的堆栈。
std::stack<GameState*> gameStates
unique_ptr
。
std::stack<std::unique_ptr<GameState>> gameStates
现在,每当我将GameState
压入堆栈时,编译器就会报错。显然我做错了什么…
它说. .
no instance of overloaded function matches the argument list
SplashScreen splashScreen1(game); //gameState object declaration
gameStates.push(std::move(&splashScreen1)); //move to stack without copying
出现错误的红线正好在gameStates.push(std::move(&splashScreen1))
.
下面std::unique_ptr
被设计为拥有堆内存,你试图给它堆栈内存。
你要做的是:
std::unique_ptr<GameState> splashScreen1Ptr(new GameState(game));
gameStates.push(std::move(splashScreen1Ptr));
相关文章:
- 检查注册表项是否链接到(或副本)另一个注册表项
- 当类型适当的构造函数可用时,为什么一个编译器尝试使用已删除的副本构造函数
- 将(临时的?)std::string传递给使用它来构造一个接受副本的对象的函数的最佳方法是什么?
- 为什么基于范围的 for 循环中的结构化绑定只是一个副本而不是引用?
- 如果静态变量只为程序的整个部分存储了一个副本,为什么我不能使用静态变量交换 2 个数字?
- 如何告诉 Cppdepend 一个目录有多个本地副本
- 提升线程问题,当一个线程与另一个线程没有相同的副本时如何共享变量?
- 一个静态变量多副本
- 我如何创建一个双重指针的深层副本,涉及其他类(C )
- C++全局常量数组:是否保证合并(优化)到一个副本中
- 为什么主可执行文件和 dlopen 加载的共享库共享命名空间静态变量的一个副本?
- 给定一个填充无符号字符**的 C 函数,如何在没有中间副本的情况下用数据填充 std::vector
- 'std::any_cast' 返回一个副本
- 构造向量时如何摆脱(一个)不必要的副本
- 移动语义是否只是一个浅层副本并将其他人的指针设置为 null?
- 创建std :: vector减去一个元素的副本的最快方法
- C 模板功能,相同类型,多个实例:共享一个相同的代码副本?即使在不同的CPP/对象文件中
- 在一个映射向量上迭代,如果满足条件,则插入一个副本
- 设计一个具有大型成员的C++类,而这些成员很少在副本中访问
- 制作一个树的持久副本(C++)