#include <iostream> #include <memory> using namespace std; class A { public: A(){} int i; int j; }; class B { public: B() = default; int i; int j; }; int main() { cout << "1" << endl; for( int i = 0; i < 3; ++i) { A* pa = new A(); B* pb = new B(); cout << "A : " << pa->i << "," << pa->j << endl; cout << "B : " << pb->i << "," << pb->j << endl; delete pa; delete pb; } cout << "2" << endl; for( int i = 0; i < 3; ++i) { shared_ptr<A> pa = make_shared<A>(); shared_ptr<B> pb = make_shared<B>(); cout << "A : " << pa->i << "," << pa->j << endl; cout << "B : " << pb->i << "," << pb->j << endl; } cout << "3" << endl; for( int i = 0; i < 3; ++i) { shared_ptr<A> pa ( new A() ); shared_ptr<B> pb ( new B() ); cout << "A : " << pa->i << "," << pa->j << endl; cout << "B : " << pb->i << "," << pb->j << endl; } cout << "4" << endl; for( int i = 0; i < 3; ++i) { shared_ptr<A> pa ( new A ); shared_ptr<B> pb ( new B ); cout << "A : " << pa->i << "," << pa->j << endl; cout << "B : " << pb->i << "," << pb->j << endl; } return 0; }
Standard input is empty
1 A : 0,0 B : 0,0 A : 163729408,0 B : 0,0 A : 163729424,0 B : 0,0 2 A : 0,0 B : 0,0 A : 0,0 B : 0,0 A : 0,0 B : 0,0 3 A : 163729408,0 B : 0,0 A : 163729408,0 B : 0,0 A : 163729408,0 B : 0,0 4 A : 163729408,0 B : 0,0 A : 163729408,0 B : 0,0 A : 163729408,0 B : 0,0