[CPP Module] 02
ํ์ํ ๊ฐ๋ ์ ๋ฆฌ
New rules
cpp02๋ฅผ ์งํํ ๋๋ ๋ฐ๋ก ๋ช ์๊ฐ ๋์ง์์ ๊ฒฝ์ฐ์๋ 4๊ฐ์ง๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ํฌํจํ๊ณ ์์ด์ผํ๋ค๊ณ ๋ช ์๊ฐ ๋์ด์๋ค.
Default constructor ( ๊ธฐ๋ณธ ์์ฑ์ )
ํด๋น ํด๋์ค์ ๊ฐ์ฒด๊ฐ ์ธ์คํด์คํ๋ ๋ ์๋์ผ๋ก ํธ์ถ๋๋ ํน์ํ ์ข ๋ฅ์ ๋ฉค๋ฒ ํจ์
(์์ฑ์๋ก ์ง์ ์ ํ ๋ ค๋ฉด class๋ช ๊ณผ ๊ฐ์ ์ด๋ฆ์ผ๋ก ํจ์๋ฅผ ์์ฑํด์ผํ๋ค. ๋ฐ๋์ธ์ x)
class Fixed
{
private:
int value;
static const int integer = 8;
public:
// **Default constructor**
Fixed();
}
๊ฐ์ฒด๊ฐ ๋ง๋ค์ด์ง๋ Default constructor๊ฐ ํธ์ถ๋๊ณ ๊ทธ๊ฒ์ ํด๋น๋๋ ๋ด์ฉ์ด ์คํ๋๋ค.
Copy constructor ( ๋ณต์ฌ ์์ฑ์ )
๊ธฐ์กด์ ์๋ ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ์ฌ ์์ฑ ํ๋ ํจ์
class Fixed
{
private:
int value;
static const int integer = 8;
public:
// **Copy constructor**
Fixed( const Fixed& fixed );
};
Fixed a;
Fixed b(a);
์์ ๊ฐ์ ํ์์ผ๋ก a๋ฅผ ์ธ์ฉํ์ฌ b๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ
Copy assignment operator ( ๋ณต์ฌ ํ ๋น ์์ฑ์ )
์ด๋ฏธ ์์ฑ๋ ๊ฐ์ฒด์ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ ๋นํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํจ์
class Fixed
{
private:
int value;
static const int integer = 8;
public:
// Copy assignment operator
Fixed &operator = (const Fixed &other);
~Fixed();
};
c = b;
์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ธฐ์กด์ ์ด๋ฏธ ์์ฑ์ด ๋์ด์๋ ๊ฐ์ฒด์ ๊ฐ์ ๋ฎ์ด์์ฐ๋ ๋ฐฉ์
Destructor ( ์๋ฉธ์ )
๊ฐ์ฒด๊ฐ ์๋ฉธ๋ ๋ ์๋์ผ๋ก ํธ์ถ์ด ๋๋ ํจ์
(class๋ช ๊ณผ ๊ฐ์ ์ด๋ฆ์ผ๋ก ํจ์์ด๋ฆ์ผ๋ก ํด์ผํ์ง๋ง ์๋ฉธ์์ ๊ฒฝ์ฐ์๋ ์์ “~”์ ๋ถ์ฌ์ผํ๋ค)
class Fixed
{
private:
int value;
static const int integer = 8;
public:
// **Destructor**
~Fixed();
};
๊ฐ์ฒด๊ฐ delete fixed; ํ์์ผ๋ก ์ญ์ ๋๊ฑฐ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ํจ์๊ฐ ๋์ด๋๋ฉด ์๋ฉธ์ ํจ์๊ฐ ์คํ๋๋ค.
ex00
์ง๊ธ ๊น์ง ์ฌ์ฉํ๋ ์ ์ํ์ด๋ ๋ถ๋ ์์์ ๊ณผ๋ ๋ค๋ฅธ ๊ณ ์ ์์์ ์ ๋ง๋๋ ๊ฒ์ด ex00์ ๋ชฉํ
cpp์์๋ ๊ณ ์ ์์์ ์ด๋ผ๋ ๊ฐ๋ ์ง์์ด ์ฝํด์ ์ง์ ๊ตฌํ์ ํ๋ ๊ณผ์ ์ ์ํจ๋ค.
ex00์์๋ ๊ณ ์ ์์์ ์ ๊ตฌํํ๊ธฐ์ ์ ๊ธฐ๋ณธ ๋ฐํ์ด ๋๋ ํจ์๋ค์ ๊ตฌํ์ด ํ์ํ๋ค.
New rules์ ์๋ 4๊ฐ์ ํจ์๊ฐ ๊ตฌํ์ด ๋์ด์ผํ๋ค.
const
int getRawBits( void ) const;
์์ ๊ฐ์ ํ์๊ฐ์ด ํด๋์ค์ ๋ฉค๋ฒ ํจ์์๋ const ๋ถ์ ์๋ ์๋ค๊ณ ํ๋ค. ์์ ๊ฐ์ด ํจ์์ const๊ฐ ๋ถ์ ์๋ฏธ๋ ๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ ์์ ํ์ง ์๋๋ค ๋ผ๋ ์๋ฏธ๋ก const๋ฅผ ๋ถ์ด๊ณ , ****์ค์ ๋ก๋ const๊ฐ ๋ถ์ ํจ์์์๋ ๋ฉค๋ฒ ๋ณ์์ ๊ฐ์ ์์ ํ ์ ์๋ค.
static
ex00์์ ๋นํธ ์๋ฅผ ์ ์ฅ์ ํ integer๋ผ๋ ๋ณ์๋ฅผ ๋ง๋ค์ด์ผํ๋๋ฐ ๊ณผ์ ์์๋ integer๋ฅผ static์ผ๋ก ๋ง๋ค๋ผ๊ณ ์ ์๊ฐ ๋์ด์๋ค.
๊ทธ๋์ class์์ ์ฌ์ฉ๋๋ static์ด ๋ฌด์์ด๊ธธ๋ static์ผ๋ก ๋ง๋ค๋ผ๊ณ ํ๋๊ฑธ๊น ๋ผ๋ ์๋ฌธ์ ์ด ์๊ฒจ ์ฐพ์๋ณด๋ class๋ด๋ถ์ ์์ฑ๋ static ๋ณ์๋ ํด๋นํด๋์ค์์ ๊ณต์ ๊ฐ ๋๋ ๋ณ์๋ผ๊ณ ํ๋ค.
// class
class Test
{
private:
static int num;
};
// ----------------------------------------------------------------------------
Test a;
Test b;
์์ ๊ฐ์ด static์ผ๋ก ์ ์ธ์ด ๋์ด์๋ค๋ฉด a์ ์๋ ๊ฐ๊ณผ b์ ์๋ ๊ฐ์ด ์๋ก ๊ณต์ ๊ฐ ๋๋ค๊ณ ์๊ฐํ๋ฉด๋๋ค.
๋์ ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ
operator overloading - cppreference.com
Customizes the C++ operators for operands of user-defined types. [edit] Syntax Overloaded operators are functions with special function names: operator op (1) operator type (2) operator new operator new [] (3) operator delete operator delete [] (4) operato
en.cppreference.com
class A;
class B;
A = B;
์์ ๊ฐ์ด class์ class๋ฅผ ‘=’ ์ฐ์ฐ์๋ฅผ ํตํด์ ๋ฎ์ด ์์ฐ๋ ค๊ณ ํ ๊ฒฝ์ฐ A์ B ๋ชจ๋ classํํ์ด๊ธฐ ๋๋ฌธ์ ๋์ ์ด ๋ถ๊ฐ๋ฅํ๋ค.
๊ทธ๋์ ์์ ๊ฐ์ ๊ฒฝ์ฐ ํด๊ฒฐ์ ํ ๋ ค๋ฉด
Class &Class::operator = ( const Class& Class )
{
}
์์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก operator๋ฅผ ์ ์ธ์ ํด์ค์ ๋ด๋ถ์ ์ผ๋ก ‘=’ ์ด๋ผ๋ ์ฐ์ฐ์๊ฐ ๋ค์ด์ค๋ฉด ํด๋น ํจ์๋ก ๋ค์ด์ค๊ฒ๋์ด ์ด๋ ํ ๋์์ ํ ๊ฒ์ธ์ง ์ง์ ์ ํด์ค ์๊ฐ ์๋ค.
๊ทธ๋์ ๊ณผ์ ์์๋ ๋์ ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ์ ๋ง๋ค์ด์ผํ๋ค.
class a( 10 );
class b = a;
์์ ๊ฐ์ ์ฝ๋์์ class b์ a๋ฅผ ๋์ ํ๋ ๊ฒฝ์ฐ์ด๋ฉด b๋ผ๋ class๊ฐ ์ฃผ์ฒด๊ฐ ๋๊ณ a๋ผ๋ ํด๋์ค๋ ๋งค๊ฐํจ์์ ๋ค์ด์ค๋ ์ธ์๊ฐ ๋๋ค๊ณ ์๊ฐ์ ํ๋ฉด ๋๋ค. ๊ทธ๋์ ์ฆ b == this ์ด๊ณ a == Class ๋ผ๊ณ ์๊ฐ์ ํด์ผํ๋ค.
์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ (operator overloading)
์ค๋ฒ๋ก๋ฉ
๋ฉ์๋์ ์ด๋ฆ์ 1๊ฐ๋ก ์ฃผ๊ณ ๋งค๊ฐ๋ณ์๋ฅผ ๋ค๋ฅด๊ฒ ํ๋ ํ์์ผ๋ก ๋ฉ์๋๋ฅผ ์ฌ๋ฌ๊ฐ ๋ง๋๋ ๊ฒ
int sum(int a, int b)
{
return a + b;
}
double sum(double a, double b)
{
return a + b;
}
์์ ๊ฐ์ด ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ฃํ์ ๋ง๊ฒ ํจ์๊ฐ ์คํ์ด ๋๋ค๊ณ ํ๋ค.
์ ๊ธฐํ๋ค..
*this์ this
this : ์๊ธฐ ์์ ์ ๊ฐ์ฒด
*this : ์๊ธฐ ์์ ์ ํด๋์ค ๊ทธ ์์ฒด
C++ this ํฌ์ธํฐ?? ๊ทธ๋ผ *this ๋ ๋ญ์ง?
โป *this์ ๋ํ ์ค๋ช ์ ๋งจ ์๋์ ์ค๋ช ํ๊ณ ์์ต๋๋ค. ๋งจ ์๋๋ง ๋ณด๋ฉด ์ดํด๊ฐ ์ ๊ฐ์ง ์์ํ ๋ ์ฒ์๋ถํฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์ฝ์ด์ฃผ์๊ธฐ ๋ฐ๋๋๋ค.^^ ๋ํ ์ค๋ช ์ค C++์ '์ฐธ์กฐ์'์ ํฌ์ธํฐ์ '๊ฐ์ ์ฐธ์กฐ' ๋ถ
hwan-shell.tistory.com
C++ 09.10 - this ํฌ์ธํฐ
this ํฌ์ธํฐ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์์ ๊ฐ์ฅ ๋ง์ ์ง๋ฌธ ์ค ํ๋๋ "ํด๋์ค์ ๋ฉค๋ฒ ํจ์๋ฅผ ํธ์ถํ ๋ C++๋ ์ด๋ป๊ฒ ํธ์ถํ ๊ฐ์ฒด(์ธ์คํด์ค)๋ฅผ ์ฐพ๋๊ฐ?" ์ด๋ค. ์ด ์ง๋ฌธ์ ๋ํ ์ ๋ต์ this๋ผ๋ ์จ๊ฒจ์ง
boycoding.tistory.com
์์ ๊ธ๋ค์ ์์ธํ๊ฒ ์ค๋ช ์ด ๋์ด์๋ ๊ฑฐ ๊ฐ๋ค.
๊ฐ๋จํ๊ฒ ์ ๋ฆฌํ์๋ฉด
this๋ ์๊ธฐ ์์ ์ ๋ํ๋ด๋ ๊ฑฐ๊ณ
*this๋ ์๊ธฐ ์์ ์ ๊ฐ์ ๋ํ๋ธ๋ค.
ex01
ex00์์ ์กฐ๊ธ ๋ ๋ฐ์ ๋์ด์ ์ถ๋ ฅ๊ณผ ๊ณ ์ ์์์ ์ ๋ํด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ๋ค๋ฃจ๋ ํํธ
roundf (from <cmath>)
float roundf(float __x);
๋ค์ด์จ ์ธ์๊ฐ์ ๋ฐ์ฌ๋ฆผํด์ฃผ๋ ํจ์
operator «
operator overloading - cppreference.com
Customizes the C++ operators for operands of user-defined types. [edit] Syntax Overloaded operators are functions with special function names: operator op (1) operator type (2) operator new operator new [] (3) operator delete operator delete [] (4) operato
en.cppreference.com
์์ ์๋ cpp๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ์งํ์ ํ์๋ค.
operator << ๋ operator = ์ ๋ง์ฐฌ๊ฐ์ง๋ก class์ ์ถ๋ ฅ์ ๋ฐ๋ก ์ง์ํ์ง์๋ค๋ณด๋ ๊ตฌํ์ด ํ์ํ๊ฒ๋๋ค.
// operator << ๊ธฐ๋ณธํํ
std::ostream& operator<<(std::ostream& os, const T& obj)
{
// write obj to stream
return os;
}
์ฌ๊ธฐ์์ std::ostream ์ ๋ฌด์์ธ๊ฐ ์ฐพ์๋ณด๋
**ostream : ์ถ๋ ฅ์ ์ํํ๋ ํด๋์ค**
**istream : ์ ๋ ฅ์ ์ํํ๋ ํด๋์ค** ๋ผ๊ณ ํ๋ค. ๊ทธ๋์ ์ฐ๋ฆฌ๊ฐ ๊ตฌํ์ ํด์ผํ๋ ๋ถ๋ถ์ด
std::cout << "a is " << a << std::endl; ๋ฅผ ๊ตฌํํด์ผํ๊ธฐ ๋๋ฌธ์ ์ถ๋ ฅ์ ํด์ฃผ๋ ํด๋์ค์ธ std::ostream์ ์ฌ์ฉํด์ฃผ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐ์ ํด์ฃผ๋ฉด ๋๋ค.
๊ทธ๋ฆฌ๊ณ ํด๋น ์ฐ์ฐ์์ ๊ฒฝ์ฐ **std::ostream**์ ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ ํจ์์ด๋ค๋ณด๋ class๋ด๋ถ์์๋ ์ ์ธ์ด ๋ถ๊ฐ๋ฅํ๋ค. ๊ทธ๋์ ํด๋์ค ๋ฐ์ธ ํค๋ํ์ผ์ ๋ฐ๋ก ์ ์๋ฅผ ํด์ฃผ์๋ค.
๊ณ ์ ์์์
์ปดํจํฐ์์์ ์ค์ ํํ: ๊ณ ์ ์์์ vs ๋ถ๋์์์
์ด์ ํธ์ํฐ ํ์๋ผ์ธ์ ๋ณด๋ค๊ฐ, '๋ถ๋์์์ ' ์ด๋ผ๋ ๋จ์ด๋ฅผ ๋ณด๋ฉด ์ด๋ป๊ฒ ํด์ํ๊ฒ ๋๋๋๋ ๊ทธ๋ฐ ํธ์์ด RT๋ก ๋์ด์๋ค. ์๊ฐํด๋ณด๋๊น ๊ต์ฅํ ๋น์ง๊ด์ ์ธ ๋จ์ด๋ค.์๋ ์์ด๋จ์ด๋ floating point
gsmesie692.tistory.com
์์ ๊ธ์ ์๋นํ ์ ๋ฆฌ๊ฐ ์ ๋์ด์๋ค.
๊ฐ๋จํ๊ฒ ์ ๋ฆฌํ์๋ฉด
์ ์๋ถ์ ์์๋ถ๋ฅผ 2์ง์๋ก ์ ์ฅ์ ํ ๋์๋ ์ ์๋ถ๋ % 2 ํํ๋ก ๋ ์ด์ ์๋๋ ์ง๋๊น์ง 2์ง์๋ก ์ ํ์ ํ๊ณ ์์๋ถ๋ * 2 ํํ๋ก 0์ด ๋์ฌ๋๊น์ง ์งํ์ ํ๋ฉฐ 2์ง์๋ก ์ ํํ์ฌ ๊ฐ์ ์ ์ฅํ๋ ๋ฐฉ์์ด๋ค.
10์ง์๋ฅผ 2์ง์๋ก ๋ฐ๊ฟ ๋ ์ ์๋ถ๋ / 2 ํํ๋ก ์งํ์ ํ๊ณ ์ค์๋ถ๋ * 2 ํํ๋ก ์งํ์ ํ๋ฉด ๋๋ค.
ํ์ง๋ง ์ด ๊ณผ์ ๋ฅผ ์งํํ ๋๋ ์์ ์ ์ ๊ธ์ฒ๋ผ ์งํ์ด๋์ง๋ ์๊ณ 10์ง์๋ฅผ ๋นํธ์ฐ์ฐ์ผ๋ก ํฌ๊ธฐ๋งํผ ๊ณฑํ์ฌ value์ ์ ์ฅํ๊ณ , ๊ฐ์ ๋ค์ ๊ฐ์ ธ์ฌ๋๋ ์ ์ฅํ ๋ ๊ณฑํ์ฌ ์ ์ฅํ์์ผ๋ ํฌ๊ธฐ๋งํผ ๋๋ ์ ๋ค์ ๊ฐ์ ๋นผ์ค๋ํํ๋ก ์งํ์ ํ๊ฒ ๋๋ค.
num << 1 // ๊ณฑํ๊ธฐ 2
num >> 1 // ๋๋๊ธฐ 2
๋นํธ์ฐ์ฐ์ ํ์ฌ ์ ์ฅํ ๋๋ ๊ณฑํ์ฌ ์ ์ฅํ๊ณ , ์ถ๋ ฅํ ๋์๋ ๋๋์ด ์ถ๋ ฅํ๋ฉด ํด๋น ๋ด์ฉ์ ์ถ๋ ฅํ ์ ์๋ค.
๋นํธ์ฐ์ฐ์ floatํํ์๋ ์ง์ ์ ์ผ๋ก ํ ์ ์๊ธฐ๋๋ฌธ์ float์ ๋นํธ์ฐ์์ด ํ์ํ๊ฒฝ์ฐ์๋ **value * (1 << size)** ํํ๋ก ์ ์ฅ์ ํ๊ณ value / (1 << size) ํํ๋ก ๋ค์ ๋นผ์ค๋ ๋ฐฉ์์ ์ฌ์ฉํ ์ ์๋ค.
์ค์๋ถ๊ฐ ๊ฐ์ง๋ ๋นํธ์๋ฆฌ์ ์
std::cout์ ๊ธฐ๋ณธ ์ ๋ฐ๋๋ 6์ด๊ธฐ๋๋ฌธ์ ๊ณ์ฐ์ํ์๋ ์์์ ์๋ฆฌ๊ฐ ์๋ฌด๋ฆฌ ๋ง์ด ๋์จ๋ค๊ณ ํด๋
์ ์์๋ฆฌ์ + ์ค์์๋ฆฌ์ = 6 ์ด ๋์ด์ผ๋งํ๋ค. ( ๊ทธ๋์ ์์ ์๋ ๋ค 6์๋ฆฌํ์์ธ ๊ฒ )
์ ์ฅ๋๋ ๊ฐ : 2^8 * N
ex02
ex01๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ex02์์๋ ๋์ฑ ๋ ๋ง์ ๋ถ๋ถ๋ค์ ๊ตฌํ์ ์๊ตฌํ๋ ํํธ์ด๋ค.
comparison operators์ arithmetic operators์ ๊ตฌํ์ ์ถ๊ฐ๋ก ์๊ตฌํ๋ ํํธ
operator overloading - cppreference.com
Customizes the C++ operators for operands of user-defined types. [edit] Syntax Overloaded operators are functions with special function names: operator op (1) operator type (2) operator new operator new [] (3) operator delete operator delete [] (4) operato
en.cppreference.com
๊ทธ๋์ ์ ๊ณผ ๊ฐ์ด ์์์๋ ๋ด์ฉ๋ค์ ๋ฐํ์ผ๋ก ๊ตฌํ์ ํ์์ต๋๋ค.
comparison operators
ํจ์์ ์ํ์ ์์ ๋๊ฐ์ด ๋์์์๊ณ , class๋ด๋ถ์ public ํํธ์ ํจ์๋ฅผ ๊ตฌํํ ๊ฒ์ด๊ธฐ๋๋ฌธ์ inside๋ถ๋ถ์ ์ฐธ๊ณ ํ์ฌ ํจ์๋ฅผ ๊ตฌํํ์๋ค.
int a = 5;
int b = 10;
class c( 5 );
class d( 10 );
std::cout << (a > b) << "\\n"; // print : 0
std::cout << (a < b) << "\\n"; // print : 1
class๋ฅผ ๋์๋น๊ตํ๋ค๋ ๊ฒ์ ๋ด๋ถ์ ๋ด๊ธฐ๋ ์ซ์๊ฐ์ ํฌ๊ธฐ๋ฅผ ๋น๊ตํ๊ฒ ๋ค๋ ์๋ฏธ๊ฐ ๋๋ฏ๋ก ๊ทธ๋ฅ ๋ณ์๋ฅผ ๋์๋น๊ตํ์๋ ๋์ค๋ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ๊ฐ์ ๋ฐํํ์์ต๋๋ค.
arithmetic operators
comparison operators์ ์ดํ๋๋ฌธ์ผ๋ก inside๋ถ๋ถ์ ์ฐธ๊ณ ํ์ฌ ํจ์๋ฅผ ๊ตฌํํ์๋ค.
int a = 5;
int b = 10;
class c( 5 );
class d( 10 );
std::cout << (a + b) << "\\n"; // print : 15
std::cout << (c + d) << "\\n"; // print : 15
์ด๊ฒ๋ ๋์๋น๊ต์ ๋ง์ฐฌ๊ฐ์ง๋ก class ๋ด๋ถ์ ๋ค์ด๊ฐ๋ ๊ฐ์ ์๋ก ๋ํด์ ๊ทธ ๊ฐ์ ์ถ๋ ฅํ๋ ํจ์์ด๋ฏ๋ก ๋ ๊ฐ์ ์๋ก ํฉ์น ์ ์๋ ์ฐ์ฐ์๋ฅผ ๊ตฌํํ๋ฉด ๋๋ ํํธ์์๋ค.
๋์๋น๊ต ์ฐ์ฐ์์์ ์ฐจ์ด์ ์ด๋ผ๋ฉด ๋์๋น๊ต ์ฐ์ฐ์๋ ๋ฉค๋ฒํจ์ ์์ฒด์ ๋ฐํ๊ฐ์ด bool ํํ์ด์ง๋ง comparison operators์ ๋ฐํ๊ฐ์ ํด๋์ค ๊ทธ ์์ฒด๋ฅผ ๋ฐํํด์ผํ๋ฏ๋ก
**์ฃผ์ฒด๊ฐ ๋๋ class (this)**์ ๊ฐ์ ๋ณํํ์ฌ์ผํ๋ค.
T T::operator+(const T2 &b) const
{
T temp = (this->num + b.num) >> n;
return (temp);
}
๊ทธ๋์ ๊ฐ์ ๋ฐ๋ก ๋ด์ง์๋ ์ด์ a์ b๋ ์์๋ก๋ง ๊ฐ์ด ๋ด๊ธฐ๋ ๊ด๊ณ์ด๋ฏ๋ก a์ b์ ๊ฐ์ด ์ง์ ์ ์ผ๋ก ๋ณํ๋ ๊ฒฝ์ฐ๋ ์์ผ๋ฏ๋ก ๋งค๊ฐํจ์์ const๋ฅผ ๋ถ์ฌ์ฃผ๊ณ ๋ด๋ถ์ ์ผ๋ก๋ ๋ฐ๋ก temp๋ฅผ ๋ฃ์ด ๊ฐ์ ๋ณํํด์ฃผ์๋ค. (๋ค๋ฅธ ์ฐ์ฐ์๋ ์ดํ๋๋ฌธ)
์ถ๊ฐ์ ์ผ๋ก ์์ ํ์์ฒ๋ผ T temp์ ๋ํ ๊ฐ์ ๋ด์ ๊ฒฝ์ฐ์๋
T::T( const int b )
{
this->num = (b << n);
}
์์์๋ ๋งค๊ฐํจ์ ๊ฐ์ ํ์์ผ๋ก ๊ฐ์ด ๋ค์ด๊ฐ๊ธฐ๋๋ฌธ์ ๋ํ ๊ฐ์ ๋ค์ ํ์ด ์ซ์๋ฅผ ์ํ ๊ทธ๋๋ก ๋ฃ์ด์ฃผ์ด์ผํ๋ค.
increment/decrement
์ฆ๊ฐ์ฐ์ฐ์์๋ ์ ์์ฐ์ฐ์์ ํ์์ฐ์ฐ์๊ฐ ์๋ค.
์ ์ค์ T T::operator++(int); ๋ ๊ทธ๋ฅ int๊ฐ ๋ถ์ด์๊ธธ๋ ๋ญ๊ฐํ๊ณ ๊ถ๊ธํด์ ๊ฒ์์ ํด๋ณด๋
โป ํ์ ์ฐ์ฐ์๋ฅผ ์ค๋ฒ๋ก๋ฉ ํ๊ธฐ ์ํด์ ๋งค๊ฐ ๋ณ์๋ก int๋ฅผ ๋๊ธฐ๋๋ฐ ์ด๋ int๋ ํน๋ณํ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ ๊ฒ์ด ์๋๋ผ ์ ์/ํ์ ์ฐ์ฐ์์ ๊ตฌ๋ถ์ ์ํ ๊ฒ์ด๋ค.
๋ผ๋ ๋ต๋ณ์ด ์์๋ค.
int a = 5, b = 5;
std::cout << ++a; // print : 6
std::cout << b++; // print : 5
์ ์ ์ฐ์ฐ์์ ํ์์ฐ์ฐ์์ ์ฐจ์ด์ ์ ์ฆ๊ฐ๋๋ ์๊ฐ๋๊ฐ ์ ์๋ ์ถ๋ ฅ์ ํ ๋ ์ฆ๊ฐ์ด ๋ ๋ค์ ๋ฐ๋ก ์ถ๋ ฅ์ด ๋๊ณ , ํ์์ ๊ฒฝ์ฐ ์ฆ๊ฐ์ด ๋๊ธฐ์ ์ถ๋ ฅ์ด ๋๊ณ ๊ทธ ์ดํ์ ์ฆ๊ฐ์ด ๋๋ค. ๊ทธ๋์ ๊ตฌํ์ ํ ๋ ๊ทธ ๋ถ๋ถ์ ์ ๊ฒฝ์จ์ ๊ตฌํ์ ํด์คฌ์ด์ผํ๋ค.
max & min
class a;
a.function();
class::max(a, b);
max๋ min ๋งค๊ฐํจ์์ ๊ฒฝ์ฐ ๋ฉ์๋์๋ ๋ค๋ฅด๊ฒ ์์ ๋ฐ๋ก ํจ์๋ก ์ฌ์ฉ์ ํด์ผํ๊ธฐ๋๋ฌธ์ static์ ์ฌ์ฉํด์ ๋งค๊ฐํจ์๋ฅผ ๋ง๋ค์ด์ผํ๋ค.
const max ์ ๊ทธ๋ฅ max๋ฅผ ๋ฐ๋ก ๋ง๋ค์ด์ผํ๋ ์ด์ ๋ const๊ฐ์ฒด๊ฐ max ํจ์๋ฅผ ๋ถ๋ฌ์ฌ๊ฒฝ์ฐ์๋ const max๊ฐ ๋ถ๋ฌ์์ง๊ณ ๊ทธ๋ฅ ๊ฐ์ฒด๊ฐ max ํจ์๋ฅผ ๋ถ๋ฌ์ฌ ๊ฒฝ์ฐ์๋ max ํจ์๊ฐ ๋ถ๋ฌ์์ง๋ค๊ณ ํ๋ค. ๊ทธ๋์ 2๊ฐ๋ฅผ ๋ฐ๋ก ๋ฐ๋ก ๊ตฌํ