Gustavo Muenz
Gustavo Muenz

Reputation: 9552

Passing a smart pointer as argument inside a class: scoped_ptr or shared_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.

Upvotes: 5

Views: 4114

Answers (5)

jturcotte
jturcotte

Reputation: 1273

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()));
      };
};

Upvotes: 0

Mykola Golubyev
Mykola Golubyev

Reputation: 59814

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());

Upvotes: 1

cmeerw
cmeerw

Reputation: 7356

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());

Upvotes: 3

sth
sth

Reputation: 229583

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());
};

Upvotes: 3

Daniel Earwicker
Daniel Earwicker

Reputation: 116664

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.

Upvotes: 1

Related Questions