Skip to content

Commit 6c0c68e

Browse files
authored
Update ITEM29.md
1 parent 75c4098 commit 6c0c68e

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

ITEM29.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,56 @@ ex)
2323
- 이동연산을 구현해야함... 안하면 어차피 빌드가 안되..
2424

2525
```c++
26-
<<  이거 애매함. 자동으로 만들어주는 경우를 못찾겠음. 그냥 복사로 대체되는듯.  >>
27-
a = std::move(item(string("이동 !!")));
28-
이동 연산이 되길 바랬지만 복사연산됨..
26+
#include
27+
#include
28+
29+
using namespace std;
30+
31+
class pm {
32+
public:
33+
int key;
34+
pm(int a):key(a) { cout << "pm() : key " << key << endl; }
35+
~pm() { cout << "~pm() : key " << key << endl; }
36+
37+
pm(pm& np) :key(np.key) {};
38+
39+
pm& operator = (pm&& rvalue) {
40+
cout << "pm move " << endl;
41+
key = rvalue.key;
42+
return *this;
43+
   }
44+
pm& operator = (pm& lvalue) {
45+
cout << "pm copy " << endl;
46+
key = lvalue.key;
47+
return *this;
48+
}
49+
};
50+
51+
class ttt {
52+
public:
53+
pm _p;
54+
string tstr;
55+
ttt(string &p, int b) :_p(b), tstr(p){};
56+
57+
};
58+
59+
int main()
60+
{
61+
ttt a(string("A객체"), 10);
62+
ttt b(string("복사연산"), 2000);
63+
ttt v(ttt(string("0"), 0));
64+
65+
//이동연산 정의가 없는경우, 컴파일러가 만들어주는 함수에 의해 v의 멤버인 pm이 어떤 결과를 리턴하는지 확인
66+
//pm이 이동이 정의되어 있어 이동연산이 일어남. ttt는 이동연산이 없음 .
67+
68+
v = std::move(b); //이동이 있는경우 이동
69+
v = b; //이동이 존재할때 복사가 있어야만 컴파일됨,
70+
}
71+
//std:move는 pm move, v=b는 pm copy가 출력된다
72+
////자동 이동연산은 멤머별 std::move로 만들어지는것 같다.
73+
2974
```
75+
3076
### 2. 이동이 더 빠르지 않다
3177
이동할 객체의 이동 연산이 해당 복사 연산보다 빠르지 않다.
3278

0 commit comments

Comments
 (0)