Member function | typical form for class C: |
์์ฑ์ | C::C(); |
์๋ฉธ์ | C::~C(); |
๋ณต์ฌ ์์ฑ์ | C::C (const C&); |
๋์ ์ฐ์ฐ์ | C& operator= (const C&); |
์ด๋ ์์ฑ์ | C::C (C&&); |
์ด๋ ๋์ ์ฐ์ฐ์ | C& operator= (C&&); |
๊ธฐ๋ณธ ์์ฑ์
๊ธฐ๋ณธ ์์ฑ์ ๋ ํด๋์ค์ ๊ฐ์ฒด๊ฐ ์ ์ธ๋์์ง๋ง ์ธ์๋ก ์ด๊ธฐํ๋์ง ์์ ๋ ํธ์ถ๋๋ ์์ฑ์์
๋๋ค .
ํด๋์ค ์ ์์ ์์ฑ์๊ฐ ์์ผ๋ฉด ์ปดํ์ผ๋ฌ๋ ํด๋์ค์ ์์์ ์ผ๋ก ์ ์๋ ๊ธฐ๋ณธ ์์ฑ์ ๊ฐ ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค . ๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด ํด๋์ค๋ฅผ ์ ์ธํ ํ:
class Example {
public:
int total;
void accumulate (int x) { total += x; }
};
์ปดํ์ผ๋ฌ๋ ๊ธฐ๋ณธ ์์ฑ์Example ๊ฐ ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค . ๋ฐ๋ผ์ ์ด ํด๋์ค์ ๊ฐ์ฒด๋ ์ธ์ ์์ด ๊ฐ๋จํ ์ ์ธํ์ฌ ์์ฑํ ์ ์์ต๋๋ค.
Example ex;
๊ทธ๋ฌ๋ ํด๋์ค์ ๋ช ์์ ์ผ๋ก ์ ์ธ๋ ๋งค๊ฐ๋ณ์ ์๋ฅผ ์ฌ์ฉํ๋ ์ผ๋ถ ์์ฑ์๊ฐ ์์ผ๋ฉด ์ปดํ์ผ๋ฌ๋ ๋ ์ด์ ์์์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ ๊ณตํ์ง ์์ผ๋ฉฐ ๋ ์ด์ ์ธ์ ์์ด ํด๋น ํด๋์ค์ ์ ๊ฐ์ฒด ์ ์ธ์ ํ์ฉํ์ง ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์ ํด๋์ค:
class Example2 {
public:
int total;
Example2 (int initial_value) : total(initial_value) { };
void accumulate (int x) { total += x; };
};
์ฌ๊ธฐ์์ type ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ์๋ฅผ ์ ์ธํ์ต๋๋ค int. ๋ฐ๋ผ์ ๋ค์ ๊ฐ์ฒด ์ ์ธ์ด ์ ํํฉ๋๋ค.
Example2 ex (100); // ok: calls constructor
๊ทธ๋ฌ๋ ๋ค์:
Example2 ex; // not valid: no default constructor
ํด๋์ค๊ฐ ํ๋์ ์ธ์๋ฅผ ์ทจํ๋ ๋ช
์์ ์์ฑ์๋ก ์ ์ธ๋์๊ณ ์๋ฌด ๊ฒ๋ ์ทจํ์ง ์๋ ์์์ ๊ธฐ๋ณธ ์์ฑ์ ๋ฅผ ๋์ฒดํ๊ธฐ ๋๋ฌธ์ ์ ํจํ์ง ์์ต๋๋ค.
๋ฐ๋ผ์ ์ด ํด๋์ค์ ๊ฐ์ฒด๊ฐ ์ธ์ ์์ด ์์ฑ๋์ด์ผ ํ๋ ๊ฒฝ์ฐ ์ ์ ํ ๊ธฐ๋ณธ ์์ฑ์ ๋ ํด๋์ค์์ ์ ์ธ๋์ด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด:
// classes and default constructors
#include <iostream>
#include <string>
using namespace std;
class Example3 {
string data;
public:
Example3 (const string& str) : data(str) {}
Example3() {}
const string& content() const {return data;}
};
int main () {
Example3 foo;
Example3 bar ("Example");
cout << "bar's content: " << bar.content() << '\n';
return 0;
}
์ฌ๊ธฐ Example3์๋ ๋น ๋ธ๋ก์ผ๋ก ์ ์๋ ๊ธฐ๋ณธ ์์ฑ์ (์ฆ, ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์)๊ฐ ์์ต๋๋ค.
Example3() {}
์ด๊ฒ์ ํด๋์ค์ ๊ฐ์ฒด๊ฐ Example3์ธ์ ์์ด ์์ฑ๋๋๋ก ํ์ฉํฉ๋๋ค( foo์ด ์์ ์์ ์ ์ธ๋ ๊ฒ์ฒ๋ผ). ์ผ๋ฐ์ ์ผ๋ก ์ด์ ๊ฐ์ ๊ธฐ๋ณธ ์์ฑ์๋ ๋ค๋ฅธ ์์ฑ์๊ฐ ์๋ ๋ชจ๋ ํด๋์ค์ ๋ํด ์์์ ์ผ๋ก ์ ์๋๋ฏ๋ก ๋ช ์์ ์ ์๊ฐ ํ์ํ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด ๊ฒฝ์ฐ Example3์๋ ๋ค๋ฅธ ์์ฑ์๊ฐ ์์ต๋๋ค.
Example3 (const string& str);
์์ฑ์๊ฐ ํด๋์ค์์ ๋ช ์์ ์ผ๋ก ์ ์ธ๋๋ฉด ์์์ ๊ธฐ๋ณธ ์์ฑ์ ๊ฐ ์๋์ผ๋ก ์ ๊ณต๋์ง ์์ต๋๋ค.
์๋ฉธ์
์๋ฉธ์๋ ์์ฑ์ ์ ๋ฐ๋ ๊ธฐ๋ฅ์ ์ํํฉ๋๋ค. ์๋ฉธ์ ๋ ํด๋์ค์ ์๋ช
์ด ์ข
๋ฃ๋ ๋ ํด๋์ค์ ํ์ํ ์ ๋ฆฌ๋ฅผ ๋ด๋นํฉ๋๋ค. ์ด์ ์ฅ์์ ์ ์ํ ํด๋์ค๋ ๋ฆฌ์์ค๋ฅผ ํ ๋นํ์ง ์์์ผ๋ฏ๋ก ์ค์ ๋ก ์ ๋ฆฌ๊ฐ ํ์ํ์ง ์์์ต๋๋ค.
๊ทธ๋ฌ๋ ์ด์ ๋ง์ง๋ง ์์ ์ ํด๋์ค๊ฐ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ก ๊ฐ์ง๊ณ ์๋ ๋ฌธ์์ด์ ์ ์ฅํ๊ธฐ ์ํด ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. ์ด ๊ฒฝ์ฐ ์ด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํด์ ํ๋ ์ญํ ์ ํ๋ ๊ฐ์ฒด์ ์๋ช
์ด ๋๋๋ฉด ์๋์ผ๋ก ํธ์ถ๋๋ ํจ์๋ฅผ ๊ฐ๋ ๊ฒ์ด ๋งค์ฐ ์ ์ฉํ ๊ฒ์
๋๋ค. ์ด๋ฅผ ์ํด ์๋ฉธ์ ๋ฅผ ์ฌ์ฉํฉ๋๋ค . ์๋ฉธ์๋ ๊ธฐ๋ณธ ์์ฑ์ ์ ๋งค์ฐ ์ ์ฌํ ๋ฉค๋ฒ ํจ์์
๋๋ค . ์ธ์๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ์๋ฌด ๊ฒ๋ ๋ฐํํ์ง ์์ต๋๋ค.void. ๋ํ ํด๋์ค ์ด๋ฆ์ ์์ฒด ์ด๋ฆ์ผ๋ก ์ฌ์ฉํ์ง๋ง ์์ ๋ฌผ๊ฒฐํ ๊ธฐํธ( ~)๊ฐ ๋ถ์ต๋๋ค.
// destructors
#include <iostream>
#include <string>
using namespace std;
class Example4 {
string* ptr;
public:
// constructors:
Example4() : ptr(new string) {}
Example4 (const string& str) : ptr(new string(str)) {}
// destructor:
~Example4 () {delete ptr;}
// access content:
const string& content() const {return *ptr;}
};
int main () {
Example4 foo;
Example4 bar ("Example");
cout << "bar's content: " << bar.content() << '\n';
return 0;
}
๊ตฌ์ฑ ์ Example4์ ๋ํ ์คํ ๋ฆฌ์ง๋ฅผ ํ ๋นํฉ๋๋ค string. ์๋ฉธ์๊ฐ ๋์ค์ ํด์ ํ๋ ์ ์ฅ์์
๋๋ค.
๊ฐ์ฒด์ ์๋ฉธ์๋ ์๋ช
์ด ๋๋๋ฉด ํธ์ถ๋ฉ๋๋ค. ์ ๊ฒฝ์ฐ ํจ์์ ๋์์ ๋ฐ์ fooํฉ๋๋ค .barmain
๋ณต์ฌ ์์ฑ์
๊ฐ์ฒด๊ฐ ์์ฒด ์ ํ์ ๋ช
๋ช
๋ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌ๋๋ฉด ๋ณต์ฌ๋ณธ์ ์์ฑํ๊ธฐ ์ํด ๋ณต์ฌ ์์ฑ์ ๊ฐ ํธ์ถ๋ฉ๋๋ค. ๋ณต์ฌ ์์ฑ์ ๋ ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๊ฐ ํด๋์ค ์์ฒด์ ๋ํ ์ ํ ์ฐธ์กฐ( ์ ๊ทํ๋ ์ ์์)์ด๊ณ ์ด ์ ํ์ ๋จ์ผ ์ธ์๋ก ํธ์ถ๋ ์
์๋ ์์ฑ์์
๋๋ค. ์๋ฅผ ๋ค์ด, ํด๋์ค ์ ๊ฒฝ์ฐ ๋ณต์ฌ ์์ฑ์ ๋ ๋ค์ ์๋ช
์ ๊ฐ์ง ์ ์์ต๋๋ค.constMyClass
MyClass::MyClass (const MyClass&);
ํด๋์ค์ ์ ์๋ ์ฌ์ฉ์ ์ ์ ๋ณต์ฌ ๋ ์ด๋ ์์ฑ์(๋๋ ํ ๋น)๊ฐ ์์ผ๋ฉด ์์์ ๋ณต์ฌ ์์ฑ์ ๊ฐ ์ ๊ณต๋ฉ๋๋ค. ์ด ๋ณต์ฌ ์์ฑ์๋ ๋จ์ํ ์์ฒด ๋ฉค๋ฒ์ ๋ณต์ฌ๋ฅผ ์ํํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ ํด๋์ค์ ๊ฒฝ์ฐ:
class MyClass {
public:
int a, b; string c;
};
์์์ ๋ณต์ฌ ์์ฑ์ ๋ ์๋์ผ๋ก ์ ์๋ฉ๋๋ค. ์ด ํจ์์ ๋ํด ๊ฐ์ ๋ ์ ์๋ ๋ค์ ๊ณผ ๋๋ต์ ์ผ๋ก ๋์ผํ ์์ ๋ณต์ฌ ๋ฅผ ์ํํฉ๋๋ค.
MyClass::MyClass(const MyClass& x) : a(x.a), b(x.b), c(x.c) {}
์ด ๊ธฐ๋ณธ ๋ณต์ฌ ์์ฑ์ ๋ ๋ง์ ํด๋์ค์ ์๊ตฌ ์ฌํญ์ ์ ํฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์์ ๋ณต์ฌExample4 ๋ ํด๋์ค ์์ฒด์ ๋ฉค๋ฒ๋ง ๋ณต์ฌํ๋ฉฐ , ์ด๋ ์ ์ฅ์ ์ฒ๋ฆฌํ๋ ํฌ์ธํฐ๊ฐ ํฌํจ๋์ด ์๊ธฐ ๋๋ฌธ์ ์์์ ์ ์ํ ํด๋์ค์ ๊ฐ์ ํด๋์ค์ ๋ํด ์์ํ ๊ฒ๊ณผ ๋ค๋ฅผ ์ ์์ต๋๋ค. ํด๋น ํด๋์ค์ ๊ฒฝ์ฐ ์์ ๋ณต์ฌ ๋ฅผ ์ํ ํ๋ค๋ ๊ฒ์ ํฌ์ธํฐ ๊ฐ์ด ๋ณต์ฌ๋์ง๋ง ๋ด์ฉ ์์ฒด๋ ๋ณต์ฌ๋์ง ์๋๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์ด๊ฒ์ ๋ ๊ฐ์ฒด(๋ณต์ฌ๋ณธ๊ณผ ์๋ณธ)๊ฐ ๋จ์ผ string๊ฐ์ฒด๋ฅผ ๊ณต์ ํ๊ณ (๋ ๋ค ๋์ผํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์์) ์ด๋ค ์์ (ํ๊ดด ์)์์ ๋ ๊ฐ์ฒด๊ฐ ๋์ผํ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ก์ ์ญ์ ํ๋ ค๊ณ ์๋ํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์๋ง๋ ๋ฐํ์์ ํ๋ก๊ทธ๋จ์ด ์ถฉ๋ํ๋ ์์ธ์ด ๋ ๊ฒ์ ๋๋ค. ์ด๊ฒ์ ๋ค์ ์ฌ์ฉ์ ์ ์๋ฅผ ์ ์ํ์ฌ ํด๊ฒฐํ ์ ์์ต๋๋ค.๊น์ ๋ณต์ฌ ๋ฅผ ์ํํ๋ ๋ณต์ฌ ์์ฑ์ :
// copy constructor: deep copy
#include <iostream>
#include <string>
using namespace std;
class Example5 {
string* ptr;
public:
Example5 (const string& str) : ptr(new string(str)) {}
~Example5 () {delete ptr;}
// copy constructor:
Example5 (const Example5& x) : ptr(new string(x.content())) {}
// access content:
const string& content() const {return *ptr;}
};
int main () {
Example5 foo ("Example");
Example5 bar = foo;
cout << "bar's content: " << bar.content() << '\n';
return 0;
}
์ด ๋ณต์ฌ ์์ฑ์ ๊ฐ ์ํ ํ๋ ์ ์ฒด ๋ณต์ฌ๋ ์๋ ๊ฐ์ฒด์ ๋ณต์ฌ๋ณธ์ ํฌํจํ๋๋ก ์ด๊ธฐํ๋ ์ ๋ฌธ์์ด์ ๋ํ ์ ์ฅ์๋ฅผ ํ ๋นํฉ๋๋ค. ์ด๋ฌํ ๋ฐฉ์์ผ๋ก ๋ ๊ฐ์ฒด(๋ณต์ฌ๋ณธ ๋ฐ ์๋ณธ)์๋ ์๋ก ๋ค๋ฅธ ์์น์ ์ ์ฅ๋ ์ฝํ ์ธ ์ ๊ณ ์ ํ ๋ณต์ฌ๋ณธ์ด ์์ต๋๋ค.
๋ณต์ฌ ์์ฑ์
๊ฐ์ฒด๋ ์ด๊ธฐํ๋ ๋ ๊ตฌ์ฑ ์ ๋ณต์ฌ๋ ๋ฟ๋ง ์๋๋ผ ๋ชจ๋ ํ ๋น ์์ ์์๋ ๋ณต์ฌํ ์ ์์ต๋๋ค. ์ฐจ์ด์ ๋ณด๊ธฐ:
MyClass foo;
MyClass bar (foo); // object initialization: copy constructor called
MyClass baz = foo; // object initialization: copy constructor called
foo = bar;
๊ตฌ์ฑ ์ ๋ฑํธ ๋ฅผ baz์ฌ์ฉํ์ฌ ์ด๊ธฐํ ๋์ง๋ง ํ ๋น ์์ ์ด ์๋๋๋ค! (๋น๋ก ํ๋์ฒ๋ผ ๋ณด์ผ ์ ์์): ๊ฐ์ฒด ์ ์ธ์ ํ ๋น ์์ ์ด ์๋๋ผ ๋จ์ผ ์ธ์ ์์ฑ์๋ฅผ ํธ์ถํ๋ ๊ตฌ๋ฌธ ์ค ํ๋์ผ ๋ฟ์ ๋๋ค. ํ ๋น ์ ํ ๋น ์์ ์ ๋๋ค. ์ฌ๊ธฐ์ ์ ์ธ๋ ๊ฐ์ฒด๊ฐ ์์ง๋ง ๊ธฐ์กด ๊ฐ์ฒด์ ๋ํด ์์ ์ด ์ํ๋๊ณ ์์ต๋๋ค. . ๋ณต์ฌ ํ ๋น ์ฐ์ฐ์ ๋ ํด๋์ค ์์ฒด์ ๊ฐ์ด๋ ์ฐธ์กฐ๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉ ํ๋ ์ค๋ฒ ๋ก๋ ์ ๋๋ค . ๋ฐํ ๊ฐ์ ์ผ๋ฐ์ ์ผ๋ก ์ ๋ํ ์ฐธ์กฐ ์ ๋๋ค(ํ์๋ ์๋์ง๋ง). ์๋ฅผ ๋ค์ด ํด๋์ค ์ ๊ฒฝ์ฐ
foofoo
operator=*thisMyClass์ฌ๋ณธ ํ ๋น ์๋ ๋ค์ ์๋ช
์ด ์์ ์ ์์ต๋๋ค.
MyClass& operator= (const MyClass&);
๋ณต์ฌ ํ ๋น ์ฐ์ฐ์ ๋ ํน์ ํจ์ ์ด๋ฉฐ ํด๋์ค์ ์ฌ์ฉ์ ์ ์ ๋ณต์ฌ ๋ ์ด๋ ํ ๋น(๋๋ ์ด๋ ์์ฑ์)์ด ์ ์๋์ง ์์ ๊ฒฝ์ฐ ์์์ ์ผ๋ก ์ ์๋ฉ๋๋ค .
๊ทธ๋ฌ๋ ์์์ ๋ฒ์ ์ ๋ง์ ํด๋์ค์ ์ ํฉํ ์์ ๋ณต์ฌExample5 ๋ฅผ ์ํํ์ง๋ง ์์์ ๊ฐ์ด ์ ์ฅ์๋ฅผ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด์ ๋ํ ํฌ์ธํฐ๊ฐ ์๋ ํด๋์ค์๋ ์ ํฉํ์ง ์์ต๋๋ค . ์ด ๊ฒฝ์ฐ ํด๋น ํด๋์ค๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด๋ฅผ ๋ ๋ฒ ์ญ์ ํ ์ํ์ด ์์ ๋ฟ๋ง ์๋๋ผ ํ ๋น ์ด์ ์ ๊ฐ์ฒด๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด๋ฅผ ์ญ์ ํ์ง ์์ ํ ๋น ์ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ๋ค. ์ด๋ฌํ ๋ฌธ์ ๋ ๋ณต์ฌ ํ ๋น ์ผ๋ก ํด๊ฒฐํ ์ ์์ต๋๋ค.์ด์ ๊ฐ์ฒด๋ฅผ ์ญ์ ํ๊ณ ์ ์ฒด ๋ณต์ฌ ๋ฅผ ์ํํฉ๋๋ค .
Example5& operator= (const Example5& x) {
delete ptr; // delete currently pointed string
ptr = new string (x.content()); // allocate space for new string, and copy
return *this;
}
๋๋ ๋ฉค๋ฒ๊ฐ ์ผ์ ํ์ง ์๊ธฐ ๋๋ฌธ์ ๋์ผํ ๊ฐ์ฒด string๋ฅผ ๋ค์ ์ฌ์ฉํ ์ ์์ต๋๋ค .string
Example5& operator= (const Example5& x) {
*ptr = x.content();
return *this;
}
๋์ ์ฐ์ฐ์
๋ณต์ฌ์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ด๋๋ ๊ฐ์ฒด์ ๊ฐ์ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ๊ฐ์ฒด์ ๊ฐ์ ์ค์ ํฉ๋๋ค. ๊ทธ๋ฌ๋ ๋ณต์ฌ์ ๋ฌ๋ฆฌ ์ฝํ
์ธ ๋ ์ค์ ๋ก ํ ๊ฐ์ฒด(์์ค)์์ ๋ค๋ฅธ ๊ฐ์ฒด(๋์)๋ก ์ ์ก๋ฉ๋๋ค. ์์ค๋ ํด๋น ์ฝํ
์ธ ๋ฅผ ์์ด๋ฒ๋ฆฌ๊ณ ๋์์ด ์ธ๊ณํฉ๋๋ค. ์ด ์ด๋์ ๊ฐ์ ์์ค๊ฐ ์ด๋ฆ ์๋ ๊ฐ์ฒด ์ธ ๊ฒฝ์ฐ์๋ง ๋ฐ์ ํฉ๋๋ค .
์ด๋ฆ ์๋ ๊ฐ์ฒด ๋ ๋ณธ์ง์ ์ผ๋ก ์ผ์์ ์ธ ๊ฐ์ฒด์ด๋ฏ๋ก ์ด๋ฆ๋ ์ง์ ๋์ง ์์์ต๋๋ค. ๋ช
๋ช
๋์ง ์์ ๊ฐ์ฒด ์ ์ผ๋ฐ์ ์ธ ์๋ ํจ์ ๋๋ ์ ํ ๋ณํ์ ๋ฐํ ๊ฐ์
๋๋ค.
๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๊ฑฐ๋ ๊ฐ์ ํ ๋นํ๊ธฐ ์ํด ์ด์ ๊ฐ์ ์์ ๊ฐ์ฒด์ ๊ฐ์ ์ฌ์ฉํ๋ ๊ฒ์ ์ค์ ๋ก ์ฌ๋ณธ์ด ํ์ํ์ง ์์ต๋๋ค. ๊ฐ์ฒด๋ ๋ค๋ฅธ ์ฉ๋๋ก ์ฌ์ฉ๋์ง ์์ผ๋ฏ๋ก ํด๋น ๊ฐ์ ๋์ ์ผ๋ก ์ด๋ํ ์ ์์ต๋๋ค. ๋ฌผ์ฒด. ์ด๋ฌํ ๊ฒฝ์ฐ๋ ์ด๋ ์์ฑ์ ์ ์ด๋ ํ ๋น ์ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค . ์ด๋ ์์ฑ์
๋ ๋ช
๋ช
๋์ง ์์ ์์๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ ์ ๊ฐ์ฒด๊ฐ ์ด๊ธฐํ๋ ๋ ํธ์ถ๋ฉ๋๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก ์ด๋ ํ ๋น ์ ๊ฐ์ฒด์ ์ด๋ฆ ์๋ ์์ ๊ฐ์ด ํ ๋น๋ ๋ ํธ์ถ๋ฉ๋๋ค.\
MyClass fn(); // function returning a MyClass object
MyClass foo; // default constructor
MyClass bar = foo; // copy constructor
MyClass baz = fn(); // move constructor
foo = bar; // copy assignment
baz = MyClass(); // move assignment
์ ์ํด ๋ฐํ๋ fn๊ฐ๊ณผ ๋ก ๊ตฌ์ฑ๋ ๊ฐ MyClass์ ๋ชจ๋ ์ด๋ฆ์ด ์ง์ ๋์ง ์์ ์์ ๊ฐ์
๋๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ ์ด๋ฆ ์๋ ๊ฐ์ฒด๋ ์๋ช
์ด ๋งค์ฐ ์งง๊ณ ์ด๊ฒ์ด ๋ ํจ์จ์ ์ธ ์์
์ผ ๋ ๋ค๋ฅธ ๊ฐ์ฒด์ ์ํด ํ๋๋ ์ ์๊ธฐ ๋๋ฌธ์ ๋ณต์ฌ๋ณธ์ ๋ง๋ค ํ์๊ฐ ์์ต๋๋ค. ์ด๋ ์์ฑ์์ ์ด๋ ํ ๋น์ ํด๋์ค ์์ฒด ์ ๋ํ rvalue ์ฐธ์กฐ
์ ํ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๋ฉค๋ฒ์
๋๋ค .
MyClass (MyClass&&); // move-constructor
MyClass& operator= (MyClass&&); // move-assignment
rvalue ์ฐธ์กฐ ๋ ๋ ๊ฐ์ ์ฐํผ์๋( )๊ฐ ์๋ ์ ํ์ ๋ฐ๋ผ ์ง์ ๋ฉ๋๋ค &&. ๋งค๊ฐ๋ณ์๋ก์ rvalue ์ฐธ์กฐ ๋ ์ด ์ ํ์ ์์ ์ธ์์ ์ผ์นํฉ๋๋ค.
์ด๋ ๊ฐ๋
์ new ๋ฐ delete๋ก ์ ์ฅ์๋ฅผ ํ ๋นํ๋ ๊ฐ์ฒด์ ๊ฐ์ด ์ฌ์ฉํ๋ ์ ์ฅ์๋ฅผ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด์ ๊ฐ์ฅ ์ ์ฉํฉ๋๋ค. ์ด๋ฌํ ๊ฐ์ฒด์์ ๋ณต์ฌ์ ์ด๋์ ์ค์ ๋ก ๋ค๋ฅธ ์์
์
๋๋ค.
- A์์ B๋ก ๋ณต์ฌ๋ ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ B์ ํ ๋น๋ ๋ค์ A์ ์ ์ฒด ๋ด์ฉ์ด B์ ํ ๋น๋ ์ด ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ณต์ฌ ๋จ์ ์๋ฏธํฉ๋๋ค.
- A์์ B๋ก ์ด๋ํ๋ ๊ฒ์ A์ ์ด๋ฏธ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ ์๋ก์ด ์คํ ๋ฆฌ์ง๋ฅผ ํ ๋นํ์ง ์๊ณ B๋ก ์ ์ก๋ฉ๋๋ค. ๊ทธ๊ฒ์ ๋จ์ํ ํฌ์ธํฐ๋ฅผ ๋ณต์ฌํ๋ ๊ฒ์ ํฌํจํฉ๋๋ค.
์๋ฅผ ๋ค์ด:
// move constructor/assignment
#include <iostream>
#include <string>
using namespace std;
class Example6 {
string* ptr;
public:
Example6 (const string& str) : ptr(new string(str)) {}
~Example6 () {delete ptr;}
// move constructor
Example6 (Example6&& x) : ptr(x.ptr) {x.ptr=nullptr;}
// move assignment
Example6& operator= (Example6&& x) {
delete ptr;
ptr = x.ptr;
x.ptr=nullptr;
return *this;
}
// access content:
const string& content() const {return *ptr;}
// addition:
Example6 operator+(const Example6& rhs) {
return Example6(content()+rhs.content());
}
};
int main () {
Example6 foo ("Exam");
Example6 bar = Example6("ple"); // move-construction
foo = foo + bar; // move-assignment
cout << "foo's content: " << foo.content() << '\n';
return 0;
}
์ปดํ์ผ๋ฌ๋ ์ด๋ฏธ ๋ฐํ ๊ฐ ์ต์ ํ ๋ก ์๋ ค์ง ์ด๋ ๊ตฌ์ฑ ํธ์ถ์ด ๊ณต์์ ์ผ๋ก ํ์ํ ๋ง์ ๊ฒฝ์ฐ๋ฅผ ์ต์ ํ ํฉ๋๋ค. ๊ฐ์ฅ ์ฃผ๋ชฉํ ๋งํ ๊ฒ์ ํจ์์์ ๋ฐํ๋ ๊ฐ์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ ๊ฒ์
๋๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ ์ด๋ ์์ฑ์ ๋ ์ค์ ๋ก ํธ์ถ๋์ง ์์ ์ ์์ต๋๋ค. rvalue ์ฐธ์กฐ
๋ ๋ชจ๋ ํจ์ ๋งค๊ฐ๋ณ์์ ์ ํ์ ์ฌ์ฉํ ์ ์์ง๋ง ์ด๋ ์์ฑ์ ์ด์ธ์ ์ฉ๋์๋ ๊ฑฐ์ ์ ์ฉํ์ง ์์ต๋๋ค . Rvalue ์ฐธ์กฐ๋ ๊น๋ค๋กญ๊ณ ๋ถํ์ํ ์ฌ์ฉ์ ์ถ์ ํ๊ธฐ ๋งค์ฐ ์ด๋ ค์ด ์ค๋ฅ์ ์์ธ์ด ๋ ์ ์์ต๋๋ค.
์์์ ๊ตฌ์ฑ์
์์์ ์ค๋ช ํ 6๊ฐ์ ํน์ ๋ฉค๋ฒ ํจ์ ๋ ํน์ ์ํฉ์์ ํด๋์ค์ ๋ํด ์์์ ์ผ๋ก ์ ์ธ๋ ๋ฉค๋ฒ์ ๋๋ค.
๋ฉค๋ฒ ํจ์ | ์์์ ์ผ๋ก ์ ์๋จ: | ๊ธฐ๋ณธ ์ ์: |
๊ธฐ๋ณธ ์์ฑ์ | ๋ค๋ฅธ ์์ฑ์๊ฐ ์๋ ๊ฒฝ์ฐ | ์๋ฌด๊ฒ๋ ํ์ง ์๋๋ค |
ํ๋ฌผ ์๊ฐ๋ก | ์๋ฉธ์๊ฐ ์๋ค๋ฉด | ์๋ฌด๊ฒ๋ ํ์ง ์๋๋ค |
๋ณต์ฌ ์์ฑ์ | ์ด๋ ์์ฑ์์ ์ด๋ ํ ๋น์ด ์๋ ๊ฒฝ์ฐ | ๋ชจ๋ ๊ตฌ์ฑ์์ ๋ณต์ฌํฉ๋๋ค. |
๊ณผ์ ๋ณต์ฌ | ์ด๋ ์์ฑ์์ ์ด๋ ํ ๋น์ด ์๋ ๊ฒฝ์ฐ | ๋ชจ๋ ๊ตฌ์ฑ์์ ๋ณต์ฌํฉ๋๋ค. |
์ด๋ ์์ฑ์ | ์๋ฉธ์, ๋ณต์ฌ ์์ฑ์ ๋ฐ ๋ณต์ฌ ๋๋ ์ด๋ ํ ๋น์ด ์๋ ๊ฒฝ์ฐ | ๋ชจ๋ ๊ตฌ์ฑ์์ ์ด๋ |
๊ณผ์ ์ด๋ | ์๋ฉธ์, ๋ณต์ฌ ์์ฑ์ ๋ฐ ๋ณต์ฌ ๋๋ ์ด๋ ํ ๋น์ด ์๋ ๊ฒฝ์ฐ | ๋ชจ๋ ๊ตฌ์ฑ์์ ์ด๋ |
๋ชจ๋ ํน์ ๋ฉค๋ฒ ํจ์ ๊ฐ ๋์ผํ ๊ฒฝ์ฐ์ ์์์ ์ผ๋ก ์ ์๋์ง ์๋ ๋ฐฉ๋ฒ์ ์ ์ํ์ญ์์ค. ์ด๋ ์ฃผ๋ก C ๊ตฌ์กฐ ๋ฐ ์ด์ C++ ๋ฒ์ ๊ณผ์ ํ์ ํธํ์ฑ ๋๋ฌธ์ด๋ฉฐ ์ค์ ๋ก ์ผ๋ถ์๋ ์ฌ์ฉ๋์ง ์๋ ๊ฒฝ์ฐ๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ๋คํ์ค๋ฝ๊ฒ๋ ๊ฐ ํด๋์ค๋ ์ด๋ฌํ ๋ฉค๋ฒ ์ค ๊ธฐ๋ณธ ์ ์๊ฐ ์๋ ๋ฉค๋ฒ ๋๋ ๋ฐ ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๊ฐ ์ญ์ ๋๋ ๋ฉค๋ฒ๋ฅผ ๋ช ์์ ์ผ๋ก ์ ํํ ์ default์์ต๋๋ค delete. ๊ตฌ๋ฌธ์ ๋ค์ ์ค ํ๋์ ๋๋ค.
function_declaration = default;
function_declaration = delete;
// default and delete implicit members
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle (int x, int y) : width(x), height(y) {}
Rectangle() = default;
Rectangle (const Rectangle& other) = delete;
int area() {return width*height;}
};
int main () {
Rectangle foo;
Rectangle bar (10,20);
cout << "bar's area: " << bar.area() << '\n';
return 0;
}
์ฌ๊ธฐ์์ Rectangle๋ ๊ฐ์ int์ธ์๋ก ๊ตฌ์ฑํ๊ฑฐ๋ ๊ธฐ๋ณธ ๊ตฌ์ฑ (์ธ์ ์์)์ผ๋ก ๊ตฌ์ฑํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด ํจ์๊ฐ ์ญ์ ๋์๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ๊ฐ์ฒด์์ ๋ณต์ฌ ์์ฑ ํ ์ ์์ต๋๋ค . Rectangle๋ฐ๋ผ์ ๋ง์ง๋ง ์์ ๊ฐ์ฒด๋ฅผ ๊ฐ์ ํ๋ฉด ๋ค์ ๋ช ๋ น๋ฌธ์ด ์ ํจํ์ง ์์ต๋๋ค.
Rectangle baz (foo);
๊ทธ๋ฌ๋ ๋ณต์ฌ ์์ฑ์๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํ์ฌ ๋ช ์์ ์ผ๋ก ์ ํจํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค.
Rectangle::Rectangle (const Rectangle& other) = default;
๋ณธ์ง์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Rectangle::Rectangle (const Rectangle& other) : width(other.width), height(other.height) {}
ํค์๋ ๋ ๊ธฐ๋ณธ ์์ฑ์default ์ ๋์ผํ ๋ฉค๋ฒ ํจ์๋ฅผ ์ ์ํ์ง ์์ง๋ง (์ฆ, ๊ธฐ๋ณธ ์์ฑ์ ๋ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์๋ฅผ ์๋ฏธํจ) ์ญ์ ๋์ง ์์ผ๋ฉด ์์์ ์ผ๋ก ์ ์๋๋ ์์ฑ์์ ๋์ผํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ๊ทธ๋ฆฌ๊ณ ๋ฏธ๋์ ํธํ์ฑ์ ์ํด ํ๋์ ๋ณต์ฌ/์ด๋ ์์ฑ์ ๋๋ ํ๋์ ๋ณต์ฌ/์ด๋ ํ ๋น์ ๋ช ์์ ์ผ๋ก ์ ์ํ์ง๋ง ๋ ๋ค ์ ์ํ์ง ์๋ ํด๋์ค๋ ๋ช ์์ ์ผ๋ก ์ ์ ํ์ง ์๋ ๋ค๋ฅธ ํน์ ๋ฉค๋ฒ ํจ์ ์ค ํ๋ ๋ฅผ ์ง์ ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
'๐ Development Study > ๐จ๐พโ๐ป C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋คํ์ฑ (0) | 2022.08.22 |
---|---|
์์ / ํ๋ ๋ (0) | 2022.08.22 |
ํด๋์ค(2) (0) | 2022.08.19 |
ํด๋์ค(1) (0) | 2022.08.19 |
๊ธฐํ ๋ฐ์ดํฐ ํ์ (0) | 2022.08.19 |
๋๊ธ