通过智能指针重置对象

发布于 2024-12-22 07:11:59 字数 308 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

带上头具痛哭 2024-12-29 07:11:59

使用 shared_ptr 时,您可以使用 reset 方法来实现此目的:

SomeObject.reset(new Object);

或者,异常安全的替代方法(您也应该将其用于初始化):

SomeObject = make_shared<Object>();

这两种方法语句使 shared_ptr 指向新创建的 Object。引用同一对象的其他 shared_ptr 不会受到影响。如果这个 shared_ptr 是唯一指向旧对象的,它将被销毁。

举例说明:

shared_ptr<int> foo = make_shared<int>(23);
shared_ptr<int> bar = foo;
std::cout << *foo << ", " << *bar << std::endl; // prints "23, 23"

*foo = 42;
std::cout << *foo << ", " << *bar << std::endl; // prints "42, 42"

bar = make_shared<int>(23);
std::cout << *foo << ", " << *bar << std::endl; // prints "42, 23"

When using a shared_ptr, you can use the reset-method for this:

SomeObject.reset(new Object);

Or, the exception-safe alternative (which you should use for the initialization as well):

SomeObject = make_shared<Object>();

Both these statements make shared_ptr point to a newly created Object. Other shared_ptr that referred to the same object will not be affected. If this shared_ptr was the only one that pointed to the old object, it will be destroyed.

To illustrate:

shared_ptr<int> foo = make_shared<int>(23);
shared_ptr<int> bar = foo;
std::cout << *foo << ", " << *bar << std::endl; // prints "23, 23"

*foo = 42;
std::cout << *foo << ", " << *bar << std::endl; // prints "42, 42"

bar = make_shared<int>(23);
std::cout << *foo << ", " << *bar << std::endl; // prints "42, 23"
静若繁花 2024-12-29 07:11:59

你到底想做什么?

  • 如果您想删除旧对象并从头开始创建一个由同一指针指向的新对象,您只需执行 SomeObject = new Object 即可。如果您愿意,可以显式重置共享指针以明确旧对象已被删除。
  • 如果您想重置对象的内容而不释放它并重新分配它,那么,只要它有一个正确实现的赋值运算符[1],向它分配默认构造的对象的习惯用法就可以了 *PObject = Object()

[1] 警告:如果对象成员本身就是智能指针,则这很重要,编译器生成的 allocateemnt 运算符通常不会执行您想要的操作。它将创建一个“浅”副本:两个对象,但它们所有的智能指针都将指向相同的对象,这意味着如果一个对象发生变化,另一个也会发生变化。

最好的方法可能是使用“重置”函数编写对象,该函数将所有字段设置为干净状态,然后(a)在构造函数中使用该函数并(b)在此时调用该函数。如果您不想这样做,那么创建一个新的游戏对象更有可能是您想要的,但无论哪种方式都可能是正确的,尽管它们在概念上非常不同,但都可以在这里工作。

What do you actually want to do?

  • If you want to delete the old object and create a new object from scratch, pointed to by the same pointer, you can just do SomeObject = new Object. If you prefer, you can explicitly reset the shared pointer to make it clear the old object is deleted.
  • If you want to reset the contents of the object without deallocating it and reallocating it, then, provided it has a correctly-implemented assignment operator[1], your idiom of assigning a default-constructed object to it will work *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.

久隐师 2024-12-29 07:11:59

这取决于您真正想要实现的目标。
您显示的代码将更改所有客户端通过 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.

冷心人i 2024-12-29 07:11:59

如果您想重新初始化对象,那么您所做的就是正确的事情。您正在使用默认构造函数创建的临时对象对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文