add construct
This commit is contained in:
parent
df371f261f
commit
fccd3f2101
14
.drone.yml
14
.drone.yml
@ -1,8 +1,12 @@
|
||||
kind: pipeline
|
||||
name: default
|
||||
steps:
|
||||
- name: test
|
||||
image: gcc:latest
|
||||
commands:
|
||||
- gcc hello.c -o hello
|
||||
- ./hello
|
||||
- name: test
|
||||
image: gcc:latest
|
||||
commands:
|
||||
- echo "*****hello*****"
|
||||
- gcc hello.cpp -o hello && ./hello
|
||||
|
||||
- cd class || exit
|
||||
- echo "*****construct*****"
|
||||
- gcc construct.cpp -o construct && ./construct
|
||||
|
81
class/construct.cpp
Normal file
81
class/construct.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user