Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <vector>
- using namespace std;
- class T1
- {
- public:
- int v;
- T1() : v(0){ log("T1 ctr1"); }
- T1(int p) : v(p) { log("T1 ctr2"); }
- ~T1() {log("T1 destr");}
- T1(const T1& other) : v(other.v) { log("T1 copy ctr"); }
- T1& operator=(const T1& other) { cout << "object: " << (long)(this) << " " << "T1 assign op2, other " << (long)(&other) << endl; v = other.v; return *this; }
- void log(const string s) { cout << "object: " << (long)(this) << " " << s << endl; }
- };
- class T2
- {
- public:
- T1* v;
- //vector<T1*> v; // uncomment for initializer_list case
- T2() : v(0){ log("T2 ctr1"); }
- T2(T1* p) : v(p) // wont work for vector
- {
- log("T2 ctr2");
- //v.push_back(p); // uncomment for initializer_list case
- }
- /*
- T2(initializer_list<T1*> list) //: v(list) // will work for vector
- {
- log("T2 ctr3");
- // v = list[0]; // wont work for single variable
- // v = list.begin(); // wont work either
- v.resize(list.size()); copy(list.begin(), list.end(), v.begin()); // will work for vector too
- }
- */
- ~T2() {log("T2 destr");}
- T2(const T2& other) : v(other.v) { log("T2 copy ctr"); }
- T2& operator=(const T2& other) { cout << "object: " << (long)(this) << " " << "T1 assign op2, other " << (long)(&other) << endl; v = other.v; return *this; }
- void log(const string s) { cout << "object: " << (long)(this) << " " << s << endl; }
- };
- int main()
- {
- cout << "test1\n";
- T1* t1 = new T1();
- cout << "test2\n";
- T1* t2 = new T1(3);
- cout << "test2.1\n";
- T1* t21 = new T1(33);
- *t21 = *t2;
- cout << "test3\n";
- T2* t3 = new T2();
- cout << "test4\n";
- T2* t4 = new T2( new T1() );
- cout << "test5\n";
- T2* t5 = new T2{ new T1(333) };
- //cout << t5->v[0]->v << endl; // test for initializer_list case
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement