dimanche 27 novembre 2016

C++ std::shared_ptr would initialized other member data in the class

My question is: when I introduce the std::shared_ptr into class B, it would give other data member the zero-initialization.

codes are given below:

class A
{
public:  
    int data_a;  
    A():data_a(0){}  
};

class B 
{
public:
    int data_b;
    A a; 
    //shared_ptr<B> ptr_b; // the key point here
};

int main()
{
    B b;
    cout << b.data_b << endl;
}

As member a has a default ctor so B would generated an implicit default ctor.
Now I didn't introduce the shared_ptr so the output would be:

-858993460

But once I introduced the share_ptr into the codes, the output became:

0

Why? What makes this happen?

I am using microsoft blend for VS community 2015 version 14.025425.01 update 3.

Aucun commentaire:

Enregistrer un commentaire