mercredi 19 décembre 2018

How does the counters work when a class is referenced by another class and with getters

Problem description

I am trying to understand how the shared_ptr counters work. In my class A when I used a getter the counter incremented by 1. When used an instance of A in class B the counter incremented by 2. When I got Done with class B the destructor of A got called twice.

What I didn't understand and what confused me is the following:

  • Why did the counter of A increment by 1 when we called the getter a.a(). Is it because we returned a pointer by Copy so now we have 2 pointers right?

  • Why do we have a counter of 3 when the desctructor of A was called?

  • In class B why did the counter increment by 2?

  • After finishing of class B how was the destructor of A called?

  • When I use reset in the both destructors it gets weirder.


Source Code

#include <iostream>
#include <memory>

using namespace std;

class A {
    public:
    A(int a):_a( make_shared<int>(a)) {}
    ~A(){/*_a.reset();*/cout <<"After destructor, number of As "<< _a.use_count() <<endl;}
    std::shared_ptr<int> a(){return _a;}
    //private:
     std::shared_ptr<int> _a;
};

class B {
    public:
    B(A a):_b( make_shared<A>(a)) {}
    ~B(){ /*_b.reset();*/ cout <<"After destructor, number of Bs "<< _b.use_count() << endl;}
     std::shared_ptr<A> b(){return _b;}
    private:
      std::shared_ptr<A> _b;
};



int main()
{  
    int number = 10;
    A a(number);
    cout <<"counter of A is " << a._a.use_count() << endl;
    cout <<"counter of A is " << a.a().use_count() << endl;

    B b(a);
    cout <<"counter of A is " << a.a().use_count() << endl;
    cout <<"counter of B is " << b.b().use_count() << endl;

    return 0;

}


Output without reset

counter of A is 1
counter of A is 2
After destructor, number of As 3
counter of A is 3
counter of B is 2
After destructor, number of Bs 1
After destructor, number of As 2
After destructor, number of As 1

Output with reset

counter of A is 1
counter of A is 2
After destructor, number of As 0
counter of A is 3
counter of B is 2
After destructor, number of As 0
After destructor, number of Bs 0
After destructor, number of As 0

Aucun commentaire:

Enregistrer un commentaire