0

my first question:

If I create a shared_ptr<class A> inside of class B constructor:

class B{
public:
    B(){
        auto a = make_shared<A>();
    }
};

will the owner of the object A be the instance of B or only the constructor of B? in other words is the lifetime of the shared_ptr be the lifetime of the the instance or when the constructor finishes the lifetime of that object will end?

my second question is:

class A takes in a raw pointer of an instance of class B in the constructor, class A has a weak_ptr<B> pointing to that object as a member variable since weak_ptr can only be used on shared_ptr and other weak_ptr and not raw pointers how do I create a weak_ptr to point to the raw pointer of instance B in my class A? right now what I am doing is:

class A{
public:
    A(class B* b){
        mB = std::shared_ptr<B>(b);
    }
private:
    std::weak_ptr<B> mB;
};

I have a hunch that this is not right specially if the lifetime of shared_ptr end with the constructor. So if there is a better way to store a raw pointer as a weak_ptr let me know. Thanks

PS. it must be a weak_ptr not a shared_ptr that is stored in class A because if the instance of class B gets destroyed I do not want it to linger around any other class which will happen if shared_ptr is used since the ref count of that object will increase.

also class B is calling class A so the instance is passed as this to the class A constructor.

tadman
  • 208,517
  • 23
  • 234
  • 262
migara
  • 55
  • 1
  • 5
  • 3
    `shared_ptr` follows the same rules that all other classes and objects follow, with respect to their lifetime. There are no special rules that apply only to `shared_ptr`. If you do a mental search-and-replace of `shared_ptr` with, let's say, an `int`, and answer the modified question yourself (since it should now be much simpler to comprehend), the answer to your original question will be exactly the same. – Sam Varshavchik Apr 17 '21 at 23:42
  • You cant make a `std::weak_ptr` point at a raw pointer, you need to start with a proper `std::shared_ptr` to begin with. Make `A`'s constructor take a `std::shared_ptr` as a parameter, and require the caller to create the `B` object using `std::make_shared()`. Don't try to do it inside `A`'s constructor. – Remy Lebeau Apr 17 '21 at 23:51
  • 1
    Why do you want a `std::weak_ptr` to a raw pointer? What are you trying to achieve with that? – Galik Apr 17 '21 at 23:57
  • @Galik Hi I am changing all my pointers in my code to smart pointers and leaving some of them as raw pointers doesn't look nice. – migara Apr 18 '21 at 00:05
  • @migara: Mindlessly following a rule is rarely an effective programming technique. Use what is meaningful for the code in question. – Nicol Bolas Apr 18 '21 at 00:07
  • Most of your pointers should probably be raw pointers (or, better, references). The only pointers that should be smart pointers are the ones responsible for (or potentially responsible for) deleting the object. – Galik Apr 18 '21 at 00:08
  • This is a very good reference: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-resource – Galik Apr 18 '21 at 00:17
  • @Galik Thanks I am kind of new to this, found a lot of resources about how to do it but not when to do it. So I assumed it is good in all situations. – migara Apr 18 '21 at 00:17
  • Try reading through the guide I linked under "Resource Management". Smart pointers are just there to make sure the object gets deleted when you are finished with it. Typically, a *dynamically created* object will have a `std::unique_ptr`managing it at a place in the software that will outlive every component that wants to use it. You will pass the raw pointer (or a reference) to functions that require it. If you can't determine which of several components may have to delete an object then you can use `std::shared_ptr` so that the last component that needs it, delets it. – Galik Apr 18 '21 at 00:23

0 Answers0