81 lines
1.8 KiB
C++
81 lines
1.8 KiB
C++
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
class A {
|
|
public:
|
|
A(int) {
|
|
cout << "A constructed at " << this << " with " << i << " at " << &i << endl;
|
|
this->i = i;
|
|
}
|
|
A() {
|
|
cout << "A constructed at " << this << endl;
|
|
this->i = 0;
|
|
}
|
|
A& operator=(const A& a) {
|
|
cout << "A assign constructed at " << this << " with a at " << &a << endl;
|
|
return *this;
|
|
}
|
|
~A() { cout << "A destructed at " << this << " with val " << i << endl; }
|
|
|
|
private:
|
|
int i;
|
|
};
|
|
|
|
class Uniq {
|
|
public:
|
|
Uniq() {}
|
|
void copy(Uniq& from) { *this = from; }
|
|
void setVal(int val) { this->val = val; }
|
|
int getVal() { return val; }
|
|
|
|
private:
|
|
Uniq(const Uniq&);
|
|
Uniq& operator=(const Uniq& from) {
|
|
cout << "Uniq hidden copy assign func called" << endl;
|
|
val = from.val;
|
|
return *this;
|
|
}
|
|
int val;
|
|
};
|
|
|
|
class Empty {
|
|
public:
|
|
Empty() { cout << "Empty constructed at " << this << endl; }
|
|
~Empty() { cout << "Empty destructed at " << this << endl; };
|
|
};
|
|
|
|
class B {
|
|
private:
|
|
int i;
|
|
A a;
|
|
|
|
public:
|
|
B(A a)
|
|
: a(a) {
|
|
cout << "B constructed at " << this << " with A at " << &a << endl;
|
|
}
|
|
~B() { cout << "B destructed at " << this << endl; }
|
|
};
|
|
|
|
int main() {
|
|
cout << "construct via new" << endl;
|
|
new Empty;
|
|
cout << "construct in sizeof()" << endl;
|
|
cout << __LINE__ << ": " << sizeof(*(new Empty)) << endl;
|
|
A a;
|
|
A aa = a;
|
|
a = (const A&)aa;
|
|
B b(a);
|
|
B bb = b;
|
|
Uniq u;
|
|
u.setVal(15);
|
|
cout << __LINE__ << ": " << u.getVal() << endl;
|
|
// Uniq u1(u);
|
|
// Uniq u2;
|
|
// u2 = u;
|
|
Uniq* u2 = new Uniq;
|
|
cout << __LINE__ << ": " << u2->getVal() << endl;
|
|
u2->copy(u);
|
|
cout << __LINE__ << ": " << u2->getVal() << endl;
|
|
} |