Passing a smart pointer as an 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 accessible to users of this class. Then this method calls other private methods within the same class and passes 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 that the correct smart pointer in this case will be scoped_ptr, however I cannot do this because scoped_ptr makes the class incompatible if it is used this way, so I have to make such methods as follows:

void privateMethod1(FooPtr& fooObj);

privateMethod1 does not save the object; it does not save references to it. Just retrieves data from the Foo class.

, , - , , , , .

, scoped_ptr.

+5
5

std:: auto_ptr, . , .

, .

, API,

void privateMethod1(const Foo& fooObj);

privateMethod1(*fooObj.get());
+1

- static_ptr , :

void privateMethod1(Foo *fooObj);

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

scoped_ptr showSomethingOnTheScreen, ( ) privateMethod1, .

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

+3

" , , ".

? , - - , , .

+1

In this case, you need to replace the distribution mechanism.
Create an object on the heap, but pass the object as a reference to private methods.

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

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

All Articles