์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ
ํด๋์ค๋ ๊ธฐ๋ณธ์ ์ผ๋ก C++ ์ฝ๋์์ ์ฌ์ฉํ ์ ์ ํ์ ์ ์ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ C++์ ์ ํ์ ๊ตฌ์ฑ ๋ฐ ํ ๋น์ ํตํด ์ฝ๋์ ์ํธ ์์ฉํ ๋ฟ๋ง ์๋๋๋ค. ๊ทธ๋ค์ ๋ํ ์ฐ์ฐ์๋ฅผ ํตํด ์ํธ ์์ฉํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๊ธฐ๋ณธ ์ ํ์ ๋ํด ๋ค์ ์์ ์ ์ํํฉ๋๋ค.
int a, b, c;
a = b + c;
์ฌ๊ธฐ์์ ๊ธฐ๋ณธํ( )์ ๋ค๋ฅธ ๋ณ์๋ int๋ํ๊ธฐ ์ฐ์ฐ์๋ฅผ ์ ์ฉํ ๋ค์ ํ ๋น ์ฐ์ฐ์๋ฅผ ์ ์ฉํฉ๋๋ค. ๊ธฐ๋ณธ ์ฐ์ ์ ํ์ ๊ฒฝ์ฐ ์ด๋ฌํ ์ฐ์ฐ์ ์๋ฏธ๋ ์ผ๋ฐ์ ์ผ๋ก ๋ช ํํ๊ณ ๋ชจํธํ์ง ์์ง๋ง ํน์ ํด๋์ค ์ ํ์์๋ ๊ทธ๋ ์ง ์์ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด:
struct myclass {
string product;
float price;
} a, b, c;
a = b + c;
b์ฌ๊ธฐ์ ๋ง์ ์ฐ์ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋ฌด์์ ํ๊ณ ๋ฌด์์ ํ๋์ง๋ ๋ถ๋ช cํ์ง ์์ต๋๋ค. ์ฌ์ค, ์ด ์ฝ๋๋ง์ผ๋ก๋ ์ ํ myclass์ ์ถ๊ฐ ๋์์ด ์ ์๋์ด ์์ง ์๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ๊ทธ๋ฌ๋ C++์์๋ ๋๋ถ๋ถ์ ์ฐ์ฐ์๊ฐ ํด๋์ค๋ฅผ ํฌํจํ์ฌ ๊ฑฐ์ ๋ชจ๋ ์ ํ์ ๋ํด ๋์์ ์ ์ํ ์ ์๋๋ก ์ค๋ฒ๋ก๋๋ ์ ์์ต๋๋ค. ๋ค์์ ์ค๋ฒ๋ก๋๋ ์ ์๋ ๋ชจ๋ ์ฐ์ฐ์ ๋ชฉ๋ก์ ๋๋ค.
+ - * / = < > += -= *= /= << >>
<<= >>= == != <= >= ++ -- % & ^ ! |
~ &= ^= |= && || %= [] () , ->* -> new
delete new[] delete[]
์ฐ์ฐ์๋ operatorํน์ ์ด๋ฆ์ ๊ฐ์ง ์ผ๋ฐ ํจ์์ธ ํจ์๋ฅผ ํตํด ์ค๋ฒ๋ก๋๋ฉ๋๋ค. ํด๋น ์ด๋ฆ์ operatorํค์๋๋ก ์์ํ๊ณ ๊ทธ ๋ค์ ์ค๋ฒ๋ก๋๋ ์ฐ์ฐ์ ๊ธฐํธ ๊ฐ ์ต๋๋ค . ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
type operator sign (parameters) { /*... body ...*/ }
์๋ฅผ ๋ค์ด, ๋ฐ์นด๋ฅดํธ ๋ฒกํฐ ๋ ๋ ์ขํ ์ธํธ x๋ฐ y์
๋๋ค. ๋ ๋ฐ์นด๋ฅดํธ ๋ฒกํฐx ์ ๋ง์
์ฐ์ฐ์ ๋ ์ขํ๋ฅผ ํจ๊ป ๋ํ๊ณ ๋ ์ขํ๋ฅผ ํจ๊ป ๋ํ๋ ๊ฒ์ผ๋ก ์ ์๋ฉ๋๋ค y. ์๋ฅผ ๋ค์ด, ๋ฐ์นด๋ฅดํธ ๋ฒกํฐ (3,1) ์ (1,2)ํจ๊ป ์ถ๊ฐํ๋ฉด (3+1,1+2) = (4,3). ์ด๊ฒ์ ๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ C++๋ก ๊ตฌํํ ์ ์์ต๋๋ค.
// overloading operators example
#include <iostream>
using namespace std;
class CVector {
public:
int x,y;
CVector () {};
CVector (int a,int b) : x(a), y(b) {}
CVector operator + (const CVector&);
};
CVector CVector::operator+ (const CVector& param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return temp;
}
int main () {
CVector foo (3,1);
CVector bar (1,2);
CVector result;
result = foo + bar;
cout << result.x << ',' << result.y << '\n';
return 0;
}
์ ๋๋ฌด ๋ง์ ๋ชจ์์ ๋ํด ํผ๋์ค๋ฝ๋ค๋ฉด CVector๊ทธ๋ค ์ค ์ผ๋ถ๋ ํด๋์ค ์ด๋ฆ(์ฆ, ์ ํ) ์ ์ฐธ์กฐ CVectorํ๊ณ ์ผ๋ถ๋ ํด๋น ์ด๋ฆ์ ๊ฐ์ง ํจ์(์ฆ, ํด๋์ค์ ๋์ผํ ์ด๋ฆ์ ๊ฐ์ ธ์ผ ํ๋ ์์ฑ์)๋ฅผ ์ฐธ์กฐํ๋ค๋ ์ ์ ๊ณ ๋ คํ์ญ์์ค. ์๋ฅผ ๋ค์ด:
CVector (int, int) : x(a), y(b) {} // function name CVector (constructor)
CVector operator+ (const CVector&); // function that returns a CVector
operator+ํด๋์ค ์ ๊ธฐ๋ฅ ์ ํด๋น ์ ํ์ ๋ํ CVector๋ํ๊ธฐ ์ฐ์ฐ์( )๋ฅผ ์ค๋ฒ๋ก๋ํฉ๋๋ค . +์ผ๋จ ์ ์ธ๋๋ฉด ์ด ํจ์๋ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์์์ ์ผ๋ก ํธ์ถํ๊ฑฐ๋ ํจ์ ์ด๋ฆ์ ๋ช ์์ ์ผ๋ก ์ฌ์ฉํ์ฌ ํธ์ถํ ์ ์์ต๋๋ค.
c = a + b;
c = a.operator+ (b);
๋ ํํ ๋ชจ๋ ๋์ผํฉ๋๋ค.
์ฐ์ฐ์ ์ค๋ฒ๋ก๋๋ ๋ชจ๋ ๋์์ ๊ฐ์ง ์ ์๋ ์ผ๋ฐ ํจ์์
๋๋ค. ์ค์ ๋ก ๊ทธ ์ค๋ฒ๋ก๋์ ์ํด ์ํ๋๋ ์ฐ์ฐ์ด ์ฐ์ฐ์์ ์ํ์ ๋๋ ์ผ๋ฐ์ ์ธ ์๋ฏธ์ ๊ด๋ จ์ด ์์ด์ผ ํ๋ค๋ ์๊ตฌ ์ฌํญ์ ์์ง๋ง ๊ฐ๋ ฅํ๊ฒ ๊ถ์ฅ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด operator+์ค์ ๋ก ๋นผ๊ธฐ ์ํด ์ค๋ฒ๋ก๋ํ๊ฑฐ๋ operator==๊ฐ์ฒด๋ฅผ 0์ผ๋ก ์ฑ์ฐ๊ธฐ ์ํด ์ค๋ฒ๋ก๋ํ๋ ํด๋์ค๋ ์ด๋ฌํ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ด๋ ค์ธ ์ ์์ง๋ง ์๋ฒฝํ๊ฒ ์ ํจํฉ๋๋ค.
๋ค์๊ณผ ๊ฐ์ ์์
์ ๋ฉค๋ฒ ํจ์ ์ค๋ฒ๋ก๋์ ํ์ํ ๋งค๊ฐ๋ณ์operator+๋น์ฐํ ์ฐ์ฐ์์ ์ค๋ฅธ์ชฝ์ ์๋ ํผ์ฐ์ฐ์์
๋๋ค. ์ด๊ฒ์ ๋ชจ๋ ์ดํญ ์ฐ์ฐ์(์ผ์ชฝ์ ํผ์ฐ์ฐ์๊ฐ ์๊ณ ์ค๋ฅธ์ชฝ์ ํ๋์ ํผ์ฐ์ฐ์๊ฐ ์๋ ์ฐ์ฐ์)์ ๊ณตํต์
๋๋ค. ๊ทธ๋ฌ๋ ์ด์์๋ ๋ค์ํ ํํ๋ก ๋ํ๋ ์ ์์ต๋๋ค. ์ฌ๊ธฐ์ ์ค๋ฒ๋ก๋๋ ์ ์๋ ๋ค๋ฅธ ์ฐ์ฐ์ ๊ฐ๊ฐ์ ํ์ํ ๋งค๊ฐ๋ณ์ ์์ฝ์ด ์๋ ํ
์ด๋ธ์ด ์์ต๋๋ค( @๊ฐ ๊ฒฝ์ฐ์ ์ฐ์ฐ์๋ก ๊ต์ฒดํ์ญ์์ค).
ํํ์ด์์๋ฉค๋ฒ ํจ์๋นํ์ ํจ์
expression | operator | member function | non member function |
@a | + - * & ! ~ ++ -- | A::operator@() | operator@(A) |
a@ | ++ -- | A::operator@(int) | operator@(A,int) |
a@b | + - * / % ^ & | < > == != <= >= << >> && || , | A::operator@(B) | operator@(A,B) |
a@b | = += -= *= /= %= ^= &= |= <<= >>= [] | A::operator@(B) | - |
a(b,c...) | () | A::operator()(B,C...) | - |
a->b | -> | A::operator->() | - |
(TYPE) a | TYPE | A::operator TYPE() | - |
์ฌ๊ธฐ์ a๋ class ์ ๊ฐ์ฒด A์ด๊ณ ๋ class ์ ๊ฐ์ฒด ์ด๊ณ ๋ bclass ์ ๊ฐ์ฒด์ ๋๋ค . ๋ชจ๋ ์ ํ์ ๋๋ค(์ฐ์ฐ์๋ type ์ผ๋ก์ ๋ณํ์ ์ค๋ฒ๋ก๋ํจ ). ์ผ๋ถ ์ฐ์ฐ์๋ ๋ฉค๋ฒ ํจ์ ๋๋ ๋น๋ฉค๋ฒ ํจ์์ ๋ ๊ฐ์ง ํ์์ผ๋ก ์ค๋ฒ๋ก๋๋ ์ ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๊ฒฝ์ฐ๋ ์ ๋ํ ์์ ์์์ ์ฌ์ฉ๋์์ต๋๋ค . ๊ทธ๋ฌ๋ ์ผ๋ถ ์ฐ์ฐ์๋ ๋น๋ฉค๋ฒ ํจ์๋ก ์ค๋ฒ๋ก๋๋ ์๋ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ ์ฐ์ฐ์ ํจ์๋ ์ ์ ํ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ทจํฉ๋๋ค. ์๋ฅผ ๋ค์ด:BcCTYPETYPE
// non-member operator overloads
#include <iostream>
using namespace std;
class CVector {
public:
int x,y;
CVector () {}
CVector (int a, int b) : x(a), y(b) {}
};
CVector operator+ (const CVector& lhs, const CVector& rhs) {
CVector temp;
temp.x = lhs.x + rhs.x;
temp.y = lhs.y + rhs.y;
return temp;
}
int main () {
CVector foo (3,1);
CVector bar (1,2);
CVector result;
result = foo + bar;
cout << result.x << ',' << result.y << '\n';
return 0;
}
The keyword this
ํค์๋ this๋ ๋ฉค๋ฒ ํจ์๊ฐ ์คํ ์ค์ธ ๊ฐ์ฒด์ ๋ํ ํฌ์ธํฐ๋ฅผ ๋ํ๋
๋๋ค. ํด๋์ค์ ๋ฉค๋ฒ ํจ์ ๋ด์์ ๊ฐ์ฒด ์์ฒด๋ฅผ ์ฐธ์กฐํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
๊ทธ ์ฉ๋ ์ค ํ๋๋ ๋ฉค๋ฒ ํจ์์ ์ ๋ฌ๋ ๋งค๊ฐ ๋ณ์๊ฐ ๊ฐ์ฒด ์์ฒด์ธ์ง ํ์ธํ๋ ๊ฒ์
๋๋ค. ์๋ฅผ ๋ค์ด:
// example on this
#include <iostream>
using namespace std;
class Dummy {
public:
bool isitme (Dummy& param);
};
bool Dummy::isitme (Dummy& param)
{
if (¶m == this) return true;
else return false;
}
int main () {
Dummy a;
Dummy* b = &a;
if ( b->isitme(a) )
cout << "yes, &a is b\n";
return 0;
}
operator=์ฐธ์กฐ๋ก ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๋ฉค๋ฒ ํจ์ ์์๋ ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค . ์ด์ ์ ๋ณธ ๋ฐ์นด๋ฅดํธ ๋ฒกํฐ ์ ๋ํ ์์ ๋ฅผ ๋ฐ๋ฅด๋ฉด ํด๋น operator=๊ธฐ๋ฅ์ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋ ์ ์์ต๋๋ค.
CVector& CVector::operator= (const CVector& param)
{
x=param.x;
y=param.y;
return *this;
}
์ฌ์ค, ์ด ํจ์๋ ์ปดํ์ผ๋ฌ๊ฐ ์ด ํด๋์ค์ ๋ํด ์์์ ์ผ๋ก ์์ฑํ๋ ์ฝ๋์ ๋งค์ฐ ์ ์ฌํฉ๋๋ค operator=.
์ ์ ๋ฉค๋ฒ
ํด๋์ค์๋ ๋ฐ์ดํฐ ๋๋ ํจ์์ ์ ์ ๋ฉค๋ฒ๊ฐ ํฌํจ๋ ์ ์์ต๋๋ค.
ํด๋์ค์ ์ ์ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ ๋์ผํ ๊ฐ์ ๊ณต์ ํ๋ ๋์ผํ ํด๋์ค์ ๋ชจ๋ ๊ฐ์ฒด์ ๋ํด ํ๋์ ๊ณตํต ๋ณ์๋ง ์๊ธฐ ๋๋ฌธ์ "ํด๋์ค ๋ณ์"๋ผ๊ณ ๋ ํฉ๋๋ค. ๋ค๋ฅธ ์ฌ๋์๊ฒ ์์
.
์๋ฅผ ๋ค์ด, ๋ค์ ์์ ์ ๊ฐ์ด ํ์ฌ ํ ๋น๋ ํด๋น ํด๋์ค์ ๊ฐ์ฒด ์๊ฐ ํฌํจ๋ ์นด์ดํฐ๋ฅผ ํฌํจํ ์ ์๋ ํด๋์ค ๋ด์ ๋ณ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
// static members in classes
#include <iostream>
using namespace std;
class Dummy {
public:
static int n;
Dummy () { n++; };
};
int Dummy::n=0;
int main () {
Dummy a;
Dummy b[5];
cout << a.n << '\n';
Dummy * c = new Dummy;
cout << Dummy::n << '\n';
delete c;
return 0;
}
์ค์ ๋ก ์ ์ ๋ฉค๋ฒ๋ ๋น๋ฉค๋ฒ ๋ณ์์ ๋์ผํ ์์ฑ์ ๊ฐ์ง๋ง ํด๋์ค ๋ฒ์๋ฅผ ์ฆ๊น๋๋ค. ์ด๋ฌํ ์ด์ ๋ก ์ฌ๋ฌ ๋ฒ ์ ์ธ๋๋ ๊ฒ์ ํผํ๊ธฐ ์ํด ํด๋์ค์์ ์ง์ ์ด๊ธฐํํ ์ ์์ง๋ง ์ธ๋ถ์์ ์ด๊ธฐํํด์ผ ํฉ๋๋ค. ์ด์ ์์์์ ๊ฐ์ด:
int Dummy::n=0;
๋์ผํ ํด๋์ค์ ๋ชจ๋ ๊ฐ์ฒด์ ๋ํ ๊ณตํต ๋ณ์ ๊ฐ์ด๊ธฐ ๋๋ฌธ์ ํด๋น ํด๋์ค์ ๋ชจ๋ ๊ฐ์ฒด์ ๋ฉค๋ฒ๋ก ์ฐธ์กฐํ๊ฑฐ๋ ํด๋์ค ์ด๋ฆ์ผ๋ก ์ง์ ์ฐธ์กฐํ ์๋ ์์ต๋๋ค(๋ฌผ๋ก ์ด๋ ์ ์ ๋ฉค๋ฒ์๋ง ์ ํจํจ).
cout << a.n;
cout << Dummy::n;
์์ ๋ ํธ์ถ์ ๋์ผํ ๋ณ์, ์ฆ ์ด ํด๋์ค์ ๋ชจ๋ ๊ฐ์ฒด๊ฐ ๊ณต์ ํ๋ nํด๋์ค ๋ด์ ์ ์ ๋ณ์๋ฅผ ์ฐธ์กฐํฉ๋๋ค . ๋ค์ ๋งํ์ง๋ง, ์ด๋ ๋น๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ง๋ง ํด๋์ค(๋๋ ๊ฐ์ฒด)์ ๋ฉค๋ฒ์ฒ๋ผ ์ก์ธ์คํด์ผ ํ๋ ์ด๋ฆ์ด ์์ต๋๋ค. ํด๋์ค๋ ์ ์ ๋ฉค๋ฒ ํจ์๋ฅผ ๊ฐ์ง ์๋ ์์ต๋๋ค. ์ด๋ค์ ๋์ผํ๊ฒ ๋ํ๋ ๋๋ค. ํด๋น ํด๋์ค์ ๋ชจ๋ ๊ฐ์ฒด์ ๊ณตํต์ธ ํด๋์ค์ ๋ฉค๋ฒ๋ก, ๋น๋ฉค๋ฒ ํจ์์ฒ๋ผ ์ ํํ๊ฒ ์๋ํ์ง๋ง ํด๋์ค์ ๋ฉค๋ฒ์ฒ๋ผ ์ก์ธ์ค๋ฉ๋๋ค. ๋น๋ฉค๋ฒ ํจ์์ ๊ฐ๊ธฐ ๋๋ฌธ์ ํด๋์ค์ ๋น์ ์ ๋ฉค๋ฒ์ ์ก์ธ์คํ ์ ์์ต๋๋ค(๋ฉค๋ฒ ๋ณ์๋ ๋ฉค๋ฒ ํจ์๋ ์๋). ๋ ๋ค ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค .Dummy
Const ๋ฉค๋ฒ ํจ์
ํด๋์ค์ ๊ฐ์ฒด๊ฐ ๊ฐ์ฒด๋ก ๊ท์ ๋๋ ๊ฒฝ์ฐ const:
const MyClass myobject;
ํด๋์ค ์ธ๋ถ์์ ๋ฐ์ดํฐ ๋ฉค๋ฒ์ ๋ํ ์ก์ธ์ค ๋ ๋ชจ๋ ๋ฐ์ดํฐ ๋ฉค๋ฒ๊ฐ constํด๋์ค ์ธ๋ถ์์ ์ก์ธ์คํ๋ ์ฌ์ฉ์๋ฅผ ์ํ ๊ฒ์ฒ๋ผ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ์ ํ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ์์ฑ์๋ ์ฌ์ ํ ํธ์ถ๋๋ฉฐ ์ด๋ฌํ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ฅผ ์ด๊ธฐํํ๊ณ ์์ ํ ์ ์์ต๋๋ค.
// constructor on const object
#include <iostream>
using namespace std;
class MyClass {
public:
int x;
MyClass(int val) : x(val) {}
int get() {return x;}
};
int main() {
const MyClass foo(10);
// foo.x = 20; // not valid: x cannot be modified
cout << foo.x << '\n'; // ok: data member x can be read
return 0;
}
๊ฐ์ฒด ์ ๋ฉค๋ฒ ํจ์ ๋ ์์ ์ด ๋ฉค๋ฒ const๋ก ์ง์ ๋ ๊ฒฝ์ฐ์๋ง ํธ์ถํ ์ ์์ต๋๋ค . const์์ ์์์ get๋ก ์ง์ ๋์ง ์์ ๋ฉค๋ฒ const๋ ์์ ํธ์ถํ ์ ์์ต๋๋ค foo. ๋ฉค๋ฒ๊ฐ ๋ฉค๋ฒ์์ ์ง์ ํ๊ธฐ const์ํด constํค์๋๋ ๋งค๊ฐ๋ณ์์ ๋ํ ๋ซ๋ ๊ดํธ ๋ค์ ํจ์ ํ๋กํ ํ์ ์ ๋ฐ๋ผ์ผ ํฉ๋๋ค.
int get() const {return x;}
const๋ฉค๋ฒ ํจ์์์ ๋ฐํ๋ ํ์์ ํ์ ํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค . ์ด๊ฒ์ const๋ฉค๋ฒ๋ฅผ ๋ก ์ง์ ํ๋ ๊ฒ๊ณผ ๋์ผํ์ง ์์ต๋๋ค const. ๋ ๋ค ๋ ๋ฆฝ์ ์ด๋ฉฐ ํจ์ ํ๋กํ ํ์ ์ ๋ค๋ฅธ ์์น์ ์์ต๋๋ค.
int get() const {return x;} // const member function
const int& get() {return x;} // member function returning a const&
const int& get() const {return x;} // const member function returning a const&
๋ก ์ง์ ๋ ๋ฉค๋ฒ ํจ์ const๋ ๋น์ ์ ๋ฐ์ดํฐ ๋ฉค๋ฒ๋ฅผ ์์ ํ๊ฑฐ๋ ๋ค๋ฅธ ๋น const๋ฉค๋ฒ ํจ์๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ๋ณธ์ง์ ์ผ๋ก const๊ตฌ์ฑ์์ ๊ฐ์ฒด์ ์ํ๋ฅผ ์์ ํด์๋ ์ ๋ฉ๋๋ค.
const๊ฐ์ฒด๋ ๋ก ํ์๋ ๋ฉค๋ฒ ํจ์์๋ง ์ก์ธ์คํ๋๋ก ์ ํ const๋์ง๋ง ๋น const๊ฐ์ฒด๋ ์ ํ๋์ง ์์ผ๋ฏ๋ก const๋น const๋ฉค๋ฒ ํจ์์ ๋ชจ๋์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
์ด์จ๋ ๊ฐ์ฒด๋ฅผ ๊ฑฐ์ ์ ์ธํ์ง ์์ ๊ฒ์ด๋ฉฐ const๋ฐ๋ผ์ ๊ฐ์ฒด๋ฅผ ์์ ํ์ง ์๋ ๋ชจ๋ ๋ฉค๋ฒ๋ฅผ const๋ก ํ์ํ๋ ๊ฒ์ ๋
ธ๋ ฅํ ๊ฐ์น๊ฐ ์๋ค๊ณ ์๊ฐํ ์ ์์ง๋ง const ๊ฐ์ฒด๋ ์ค์ ๋ก ๋งค์ฐ ์ผ๋ฐ์ ์
๋๋ค. ํด๋์ค๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ๋ ๋๋ถ๋ถ์ ํจ์๋ ์ค์ ๋ก ์ฐธ์กฐ๋ก ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ์ด๋ฌํ ํจ์๋ ํด๋น ๋ฉค๋ฒ const์๋ง ์ก์ธ์คํ ์ ์์ต๋๋ค .const
// const objects
#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass(int val) : x(val) {}
const int& get() const {return x;}
};
void print (const MyClass& arg) {
cout << arg.get() << '\n';
}
int main() {
MyClass foo (10);
print(foo);
return 0;
}
์ด ์์์ ๊ฐ ๋ฉค๋ฒ get๋ก ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ ๊ฐ์ฒด๋ ๋ฉค๋ฒ ํจ์ ์๋ง ์ก์ธ์คํ ์ ์์ผ๋ฏ๋ก ํจ์ ์์ ์ ๋ํ ํธ์ถ ์ด ๋ถ๊ฐ๋ฅ ํฉ๋๋ค. ๋ฉค๋ฒ ํจ์๋ constness์ ๋ฐ๋ผ ์ค๋ฒ๋ก๋๋ ์ ์์ต๋๋ค. ์ฆ, ํ๋๋ is ์ด๊ณ ๋ค๋ฅธ ํ๋๋ ๊ทธ๋ ์ง ์๋ค๋ ์ ์ ์ ์ธํ๊ณ ํด๋์ค๋ ๋์ผํ ์๋ช ์ ๊ฐ์ง ๋ ๊ฐ์ ๋ฉค๋ฒ ํจ์๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ ๋ฒ์ ์ ๊ฐ์ฒด ์์ฒด๊ฐ const์ด๊ณ non - version์ ๊ฐ์ฒด ์์ฒด๊ฐ non-์ผ ๋ ํธ์ถ๋ฉ๋๋ค .constarg.get()printconstconst
// overloading members on constness
#include <iostream>
using namespace std;
class MyClass {
int x;
public:
MyClass(int val) : x(val) {}
const int& get() const {return x;}
int& get() {return x;}
};
int main() {
MyClass foo (10);
const MyClass bar (20);
foo.get() = 15; // ok: get() returns int&
// bar.get() = 25; // not valid: get() returns const int&
cout << foo.get() << '\n';
cout << bar.get() << '\n';
return 0;
}
ํด๋์ค ํ ํ๋ฆฟ
ํจ์ ํ ํ๋ฆฟ์ ์์ฑํ ์ ์๋ ๊ฒ์ฒ๋ผ ํด๋์ค ํ ํ๋ฆฟ์ ์์ฑํ์ฌ ํด๋์ค๊ฐ ํ ํ๋ฆฟ ๋งค๊ฐ๋ณ์๋ฅผ ์ ํ์ผ๋ก ์ฌ์ฉํ๋ ๋ฉค๋ฒ๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด:
template <class T>
class mypair {
T values [2];
public:
mypair (T first, T second)
{
values[0]=first; values[1]=second;
}
};
๋ฐฉ๊ธ ์ ์ํ ํด๋์ค๋ ์ ํจํ ์ ํ์ ๋ ์์๋ฅผ ์ ์ฅํ๋ ์ญํ ์ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ์ด ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์ ์ธํ์ฌ int๊ฐ์ด 115์ 36์ธ ๋ ๊ฐ์ ์ ์ ๊ฐ ์ ํ์ ์ ์ฅํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํฉ๋๋ค.
mypair<int> myobject (115, 36);
์ด ๋์ผํ ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ ๋ค๋ฅธ ์ ํ์ ์ ์ฅํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ์๋ ์์ต๋๋ค.
mypair<double> myfloats (3.0, 2.18);
์์ฑ์๋ ์ด์ ํด๋์ค ํ ํ๋ฆฟ์ ์ ์ผํ ๋ฉค๋ฒ ํจ์์ด๋ฉฐ ํด๋์ค ์ ์ ์์ฒด ๋ด์์ ์ธ๋ผ์ธ์ผ๋ก ์ ์๋์์ต๋๋ค. ๋ฉค๋ฒ ํจ์๊ฐ ํด๋์ค ํ ํ๋ฆฟ์ ์ ์ ์ธ๋ถ์ ์ ์๋ ๊ฒฝ์ฐ template <...>์ ๋์ฌ๊ฐ ์์ ์์ผ ํฉ๋๋ค.
// class templates
#include <iostream>
using namespace std;
template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second)
{a=first; b=second;}
T getmax ();
};
template <class T>
T mypair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}
int main () {
mypair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}
๋ฉค๋ฒ ํจ์ ์ ์ ๊ตฌ๋ฌธ์ ํ์ธํ์ธ์ getmax.
template <class T>
T mypair<T>::getmax ()
๋๋ฌด ๋ง์ T's์ ์ํด ํผ๋? ์ด ์ ์ธ ์๋ ์ธ ๊ฐ์ T's๊ฐ ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ๋ ํ ํ๋ฆฟ ๋งค๊ฐ๋ณ์์ ๋๋ค. ๋ ๋ฒ์งธ T๋ ํจ์์์ ๋ฐํ๋ ์ ํ์ ๋ํ๋ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ธ ๋ฒ์งธ T(๊บพ์ ๊ดํธ ์ฌ์ด)๋ ์๊ตฌ ์ฌํญ์ ๋๋ค. ์ด ํจ์์ ํ ํ๋ฆฟ ๋งค๊ฐ ๋ณ์๋ ํด๋์ค ํ ํ๋ฆฟ ๋งค๊ฐ ๋ณ์์์ ์ง์ ํฉ๋๋ค.
ํ ํ๋ฆฟ ์ ๋ฌธํ
ํน์ ์ ํ์ด ํ
ํ๋ฆฟ ์ธ์๋ก ์ ๋ฌ๋ ๋ ํ
ํ๋ฆฟ์ ๋ํด ๋ค๋ฅธ ๊ตฌํ์ ์ ์ํ ์ ์์ต๋๋ค. ์ด๊ฒ์ ํ
ํ๋ฆฟ ์ ๋ฌธํ ๋ผ๊ณ ํฉ๋๋ค . ์๋ฅผ ๋ค์ด, ๋ชจ๋ ์ ํ์ ์์ ํ๋๋ฅผ ์ ์ฅํ ์ ์๊ณ ํด๋น ๊ฐ์ ์ฆ๊ฐ์ํค๋ ๋ผ๋ ๋ฉค๋ฒ ํจ์๊ฐ ํ๋๋ง
์๋ ๋งค์ฐ ๊ฐ๋จํ ํด๋์ค๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค . ๊ทธ๋ฌ๋ ์ ํ์ ์์๋ฅผ ์ ์ฅํ ๋ ํจ์ ๋ฉค๋ฒ๋ฅผ ์ฌ์ฉํ์ฌ ์์ ํ ๋ค๋ฅธ ๊ตฌํ์ ๊ฐ๋ ๊ฒ์ด ๋ ํธ๋ฆฌ ํ๋ค๋ ๊ฒ์ ์๊ฒ ๋์๊ธฐ ๋๋ฌธ์ ํด๋น ์ ํ์ ๋ํ ํด๋์ค ํ
ํ๋ฆฟ ์ ๋ฌธํ๋ฅผ ์ ์ธํ๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค.mycontainerincreasecharuppercase
// template specialization
#include <iostream>
using namespace std;
// class template:
template <class T>
class mycontainer {
T element;
public:
mycontainer (T arg) {element=arg;}
T increase () {return ++element;}
};
// class template specialization:
template <>
class mycontainer <char> {
char element;
public:
mycontainer (char arg) {element=arg;}
char uppercase ()
{
if ((element>='a')&&(element<='z'))
element+='A'-'a';
return element;
}
};
int main () {
mycontainer<int> myint (7);
mycontainer<char> mychar ('j');
cout << myint.increase() << endl;
cout << mychar.uppercase() << endl;
return 0;
}
๋ค์์ ํด๋์ค ํ ํ๋ฆฟ ์ ๋ฌธํ์ ์ฌ์ฉ๋๋ ๊ตฌ๋ฌธ์ ๋๋ค
template <> class mycontainer <char> { ... };
์ฐ์ , template<>๋น ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก์ ํฌํจํ์ฌ ํด๋์ค ์ด๋ฆ ์์ ๊ฐ ํ์๋ฉ๋๋ค. ์ด๋ ๋ชจ๋ ์ ํ์ด ์๋ ค์ ธ ์๊ณ ์ด ์ ๋ฌธํ์ ํ
ํ๋ฆฟ ์ธ์๊ฐ ํ์ํ์ง ์๊ธฐ ๋๋ฌธ์
๋๋ค. ๊ทธ๋ฌ๋ ์ฌ์ ํ ํด๋์ค ํ
ํ๋ฆฟ์ ์ ๋ฌธํ์ด๋ฏ๋ก ๊ทธ ์์ฒด๋ก ์ฃผ์ํด์ผ ํฉ๋๋ค.
๊ทธ๋ฌ๋ ์ด ์ ๋์ฌ๋ณด๋ค ๋ ์ค์ํ ๊ฒ์ <char>ํด๋์ค ํ
ํ๋ฆฟ ์ด๋ฆ ๋ค์ ์ ๋ฌธํ ๋งค๊ฐ๋ณ์์
๋๋ค. ์ด ํน์ํ ๋งค๊ฐ๋ณ์ ์์ฒด๋ ํ
ํ๋ฆฟ ํด๋์ค๊ฐ ํน์ํ๋๋ ์ ํ์ ์๋ณํฉ๋๋ค( char). ์ผ๋ฐ ํด๋์ค ํ
ํ๋ฆฟ๊ณผ ์ ๋ฌธํ ๊ฐ์ ์ฐจ์ด์ ์ ํ์ธํ์ธ์.
template <class T> class mycontainer { ... };
template <> class mycontainer <char> { ... };
์ฒซ ๋ฒ์งธ ์ค์ ์ผ๋ฐ ํ
ํ๋ฆฟ์ด๊ณ ๋ ๋ฒ์งธ ์ค์ ์ ๋ฌธํ์
๋๋ค.
ํ
ํ๋ฆฟ ํด๋์ค์ ๋ํ ์ ๋ฌธํ๋ฅผ ์ ์ธํ ๋ ์ผ๋ฐ ํ
ํ๋ฆฟ์์ ์ ๋ฌธํ๋ก์ "์์" ๋ฉค๋ฒ๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ผ๋ฐ ํ
ํ๋ฆฟ ํด๋์ค์ ๋์ผํ ๊ฒฝ์ฐ์๋ ๋ชจ๋ ํด๋น ๋ฉค๋ฒ๋ฅผ ์ ์ํด์ผ ํฉ๋๋ค.
'๐ Development Study > ๐จ๐พโ๐ป C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์์ / ํ๋ ๋ (0) | 2022.08.22 |
---|---|
์์ฑ์/์๋ฉธ์ (0) | 2022.08.22 |
ํด๋์ค(1) (0) | 2022.08.19 |
๊ธฐํ ๋ฐ์ดํฐ ํ์ (0) | 2022.08.19 |
๊ตฌ์กฐ์ฒด (0) | 2022.08.19 |
๋๊ธ