通过智能指针重置对象
我是 C++ 新手,我想知道重建/重新初始化对象的最佳方法是什么?我的最小代码如下所示:
typedef boost::shared_ptr<Object> PObject;
int main()
{
PObject SomeObject;
SomeObject = PObject(new Object);
// some code
*SomeObject = Object();
}
我觉得这不是正确的方法。有人可以告诉我最好的方法吗?我只想知道如何重建/重新初始化一个对象。谢谢!
I'm new to C++ and I'm wondering what's the best way to reconstruct/reinitialize an object? My minimal code looks like this:
typedef boost::shared_ptr<Object> PObject;
int main()
{
PObject SomeObject;
SomeObject = PObject(new Object);
// some code
*SomeObject = Object();
}
I feel that's not the right way to do it. Could someone show me the best way to do it? I just want to know how to reconstruct/reinitialize an object. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
shared_ptr
时,您可以使用reset
方法来实现此目的:或者,异常安全的替代方法(您也应该将其用于初始化):
这两种方法语句使
shared_ptr
指向新创建的Object
。引用同一对象的其他shared_ptr
不会受到影响。如果这个shared_ptr
是唯一指向旧对象的,它将被销毁。举例说明:
When using a
shared_ptr
, you can use thereset
-method for this:Or, the exception-safe alternative (which you should use for the initialization as well):
Both these statements make
shared_ptr
point to a newly createdObject
. Othershared_ptr
that referred to the same object will not be affected. If thisshared_ptr
was the only one that pointed to the old object, it will be destroyed.To illustrate:
你到底想做什么?
SomeObject = new Object
即可。如果您愿意,可以显式重置共享指针以明确旧对象已被删除。*PObject = Object()
[1] 警告:如果对象成员本身就是智能指针,则这很重要,编译器生成的 allocateemnt 运算符通常不会执行您想要的操作。它将创建一个“浅”副本:两个对象,但它们所有的智能指针都将指向相同的对象,这意味着如果一个对象发生变化,另一个也会发生变化。
最好的方法可能是使用“重置”函数编写对象,该函数将所有字段设置为干净状态,然后(a)在构造函数中使用该函数并(b)在此时调用该函数。如果您不想这样做,那么创建一个新的游戏对象更有可能是您想要的,但无论哪种方式都可能是正确的,尽管它们在概念上非常不同,但都可以在这里工作。
What do you actually want to do?
SomeObject = new Object
. If you prefer, you can explicitly reset the shared pointer to make it clear the old object is deleted.*PObject = Object()
[1] Warning: This is non-trivial if the object members are themselves smart pointers, the compiler generated assignemnt operator doesn't usually do what you want. It will create a "shallow" copy: two objects, but all their smart pointers will point to the same objects, which means if one changes, the other will also change.
Probably the best is to write your object with a "reset" function which sets all the fields to a clean state, and then (a) use that function in the constructor and (b) call that function in this point. If you don't want to do that, creating a new game object is more likely to be what you want, but either way could be correct, and even though they're very different conceptually, either may work here.
这取决于您真正想要实现的目标。
您显示的代码将更改所有客户端通过
shared_ptr
指向的值。如果这就是你想做的,那么你的方法很好。如果您想为一位客户获得一件新物品。
shared_ptr::reset
是正确的方法。但是,在 C++ 中,值通常优先于共享对象。除非有充分的理由实际使用免费商店,否则不要这样做。
It depends on what you actually want to achieve.
The code you are showing will change the value pointed to through the
shared_ptr
for all clients. If that is what you want to do, your way is good.If you want to get a new object for one client.
shared_ptr::reset
is the right way to do it.However, in C++, values are generally preferred over shared objects. Unless there is a good reason to actually use the free store, don't do it.
如果您想重新初始化对象,那么您所做的就是正确的事情。您正在使用默认构造函数创建的临时对象对 SomeObject 指向的原始对象调用赋值运算符。
What you are doing is the correct thing if you want to reinitialize the object. You are calling the assignment operator on the original object pointed to by SomeObject with a temporary Object created with the default constructor.