在类中传递智能指针作为参数:scoped_ptr 还是shared_ptr?

发布于 2024-07-15 00:52:27 字数 737 浏览 8 评论 0原文

我有一个类,它在一个公共方法中创建一个对象。 该对象是私有的,对该类的用户不可见。 然后,此方法调用同一类中的其他 private 方法,并将创建的对象作为参数传递:

class Foo {
   ...
};

class A {
   private:
      typedef scoped_ptr<Foo> FooPtr;

      void privateMethod1(FooPtr fooObj);

   public:
      void showSomethingOnTheScreen() {
          FooPtr fooObj(new Foo);
          privateMethod1(fooObj);
      };
};

我相信在这种情况下正确的智能指针将是scoped_ptr,但是,我不能这样做,因为如果以这种方式使用,scoped_ptr 会使类不可复制,所以我应该像这样创建方法:

void privateMethod1(FooPtr& fooObj);

privateMethod1 不存储对象,也不保留它的引用。 仅从 Foo 类中检索数据。

正确的方法可能是根本不使用智能指针并在堆栈中分配对象,但这是不可能的,因为它使用的库不允许对象位于堆栈上,它们必须位于堆上。

毕竟,我仍然对scoped_ptr的真正用法感到困惑。

I have a class that creates an object inside one public method. The object is private and not visible to the users of the class. This method then calls other private methods inside the same class and pass the created object as a parameter:

class Foo {
   ...
};

class A {
   private:
      typedef scoped_ptr<Foo> FooPtr;

      void privateMethod1(FooPtr fooObj);

   public:
      void showSomethingOnTheScreen() {
          FooPtr fooObj(new Foo);
          privateMethod1(fooObj);
      };
};

I believe the correct smart pointer in this case would be a scoped_ptr, however, I can't do this because scoped_ptr makes the class non copyable if used that way, so should I make the methods like this:

void privateMethod1(FooPtr& fooObj);

privateMethod1 doesn't store the object, neither keeps references of it. Just retrieves data from the class Foo.

The correct way would probably be not using a smart pointer at all and allocating the object in the stack, but that's not possible because it uses a library that doesn't allow objects on the stack, they must be on the Heap.

After all, I'm still confused about the real usage of scoped_ptr.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

闻呓 2024-07-22 00:52:29

这种情况下,只需更换分配机制即可。
在堆上创建对象,但将该对象作为私有方法的引用传递。

class A {
   private:
      void privateMethod1(Foo& fooObj);

   public:
      void showSomethingOnTheScreen() {
          scoped_ptr<Foo> fooObj(new Foo);
          privateMethod1(*(fooObj.get()));
      };
};

In that case, you only have to replace the allocation mechanism.
Create the object on the heap, but pass the object as reference to private methods.

class A {
   private:
      void privateMethod1(Foo& fooObj);

   public:
      void showSomethingOnTheScreen() {
          scoped_ptr<Foo> fooObj(new Foo);
          privateMethod1(*(fooObj.get()));
      };
};
温柔女人霸气范 2024-07-22 00:52:29

我对“它使用的库不允许对象位于堆栈上,它们必须位于堆上”的评论表示怀疑。

为什么? 这通常意味着它们必须以某种特殊的方式释放——所以也许这些解决方案都不起作用。

I'm suspicious about the comment "it uses a library that doesn't allow objects on the stack, they must be on the Heap."

Why? That typically means that they must be deallocated in some special way - so perhaps none of these solutions will work.

烟若柳尘 2024-07-22 00:52:29

此处使用简单的 std::auto_ptr 因为您无法在堆栈上创建对象。 最好你的私有函数只接受原始指针。

真正的用法是,您不必捕获所有可能的异常并进行手动删除。

事实上,如果您的对象不修改对象并且您的 API 返回对象,那么您最好使用

void privateMethod1(const Foo& fooObj);

该对象并将其传递给那里

privateMethod1(*fooObj.get());

Use here simple std::auto_ptr as you can't create objects on the stack. And it is better to your private function just simply accept raw pointer.

Real usage is that you don't have to catch all possible exceptions and do manual delete.

In fact if your object is doesn't modify object and your API return object for sure you'd better to use

void privateMethod1(const Foo& fooObj);

and pass the object there as

privateMethod1(*fooObj.get());
抚笙 2024-07-22 00:52:28

我会在 showSomethingOnTheScreen 中使用scoped_ptr,但将原始指针(或引用)传递给privateMethod1,例如

scoped_ptr; fooObj(new Foo);
privateMethod1(fooObj.get());

I would use scoped_ptr inside showSomethingOnTheScreen, but pass a raw pointer (or reference) to privateMethod1, e.g.

scoped_ptr<Foo> fooObj(new Foo);
privateMethod1(fooObj.get());

香橙ぽ 2024-07-22 00:52:27

另一种可能性是将对象创建为 static_ptr 以方便内存管理,但只需将原始指针传递给其他私有方法:

void privateMethod1(Foo *fooObj);

void showSomethingOnTheScreen() {
  scoped_ptr<Foo> fooObj(new Foo);
  privateMethod1(fooObj.get());
};

One further possibility is to create the object as a static_ptr for ease of memory management, but just pass the raw pointer to the other private methods:

void privateMethod1(Foo *fooObj);

void showSomethingOnTheScreen() {
  scoped_ptr<Foo> fooObj(new Foo);
  privateMethod1(fooObj.get());
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文