My question is:
When I introduce the
//shared_ptr<B> ptr_b; // the key point here
cout << b.data_b << endl;
The implicit-default constructor will default-initialize your scalar data members, so they will have an indeterminate value. As for your test case, it has undefined behavior as you're trying to print an uninitialized object. Any output whatsoever is valid.