#include <iostream>
using namespace std;
int Adder(int num1=10, int num2=20);
int main(void)
{
cout << Adder() << endl; // 30
cout << Adder(3) << endl; // 23
cout << Adder(3, 5) << endl; // 8
return 0;
}
int Adder(int num1, int num2)
{
return num1 + num2;
}
#define SQUARE(x) ((x)*(x))
inline int SQUARE(x)
{
return x * x;
}
#include <iostream>
using namespace std;
/*
// int 로 됐을 때 다른 자료 형은 ? 매크로 함수는 다 되는데 ..? 이점이 없다
inline int SQUARE(int x)
{
return x * x;
}
*/
template <typename T>
inline T SQUARE(T x)
{
return x * x;
}
int main(void)
{
cout << SQUARE(5) << endl;
cout << SQUARE(12) << endl;
return 0;
}
#include <iostream>
namespace BestComp
{
void SimpleFunc(void)
{
std::cout << "Best Comp Simple Func" << std::endl;
}
}
namespace ProgComp
{
void SimpleFunc(void)
{
std::cout << "Prog Comp Simple Func" << std::endl;
}
}
int main(void)
{
BestComp::SimpleFunc();
ProgComp::SimpleFunc();
}
<또 다른 선언 방법>
#include <iostream>
namespace BestComp
{
void SimpleFunc(void);
}
namespace ProgComp
{
void SimpleFunc(void);
}
int main(void)
{
BestComp::SimpleFunc();
ProgComp::SimpleFunc();
}
void BestComp::SimpleFunc(void)
{
std::cout << "Best Comp Simple Func" << std::endl;
}
void ProgComp::SimpleFunc(void)
{
std::cout << "Best Comp Simple Func" << std::endl;
}
선언과 구현의 분리한 표현
Name alias
#include <iostream>
namespace ABC = AAA::BBB::CC
ABC::num1 = 10;
ABc::num2 = 20;
#include <iostream>
using namespace std;
int main(void)
{
int num1 = 1020;
int &num2 = num1; // 반드시 초기화 ! 안하면 Complie Error
num2 = 3047;
cout << "Val : " << num1 << endl;
cout << "Ref : " << num2 << endl;
cout << "Val : " << &num1 << endl;
cout << "Ref : " << &num2 << endl;
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
int num = 12;
int *ptr = #
int **dptr = &ptr;
int &ref = num;
int *(&pref) = ptr;
int **(&dpref) = dptr;
cout << ref << endl;
cout << *pref << endl;
cout << **dpref << endl;
return 0;
}
포인터 변수도 변수이므로 참조자의 선언이 가능하다.
const 참조자
int Adder(const int &num1, const int &num2)
{
return num1 + num2;
}
const int num = 20;
const int &ref = num;
주소 값을 인자로 전달하는 함수의 호출 방식
외부 변수에 저장된 값을 변경하기 위해 필요하다.
void SwapByRef(int * ptr1, int * ptr2)
{
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
위의 함수에서는 두 개 주소 값을 받아서 그 주소 값이 참조하는 영역에 저장된 값을 직접 변경하고 있다.
Call-by-Address ?
#include <iostream>
using namespace std;
void SwapByRef2(int &ref1, int& ref2)
{
int temp = ref1;
ref1 = ref2;
ref2 = temp;
}
int main(void)
{
int val1=10, val2=20;
SwapByRef2(val1, val2);
cout << "val1 : " << val1 << endl;
cout << "val2 : " << val2 << endl;
return 0;
}
void HappyFunc(const int &ref)
// 함수 내에서 참조자 ref를 이용한 값의 변경은 하지 않겠다는 의미군 !\
int& RefRetFuncOne(int &ref) { return ref } // 1.
int RefRetFuncOne(int &ref) { return ref } // 2.
#include <iostream>
using namespace std;
int& RefRetFuncOne(int &ref)
{
ref++;
return ref;
}
int main(void)
{
int num1 = 1;
int &num2 = RefRetFuncOne(num1);
//~ int num2 = RefRetFuncOne(num1); // 비교
num1++;
num2++;
cout << "num1 : " << num1 << endl;
cout << "num2 : " << num2 << endl;
return 0;
}
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
char * MakeStrAdr(int len)
{
// char * str = (char*)malloc(sizeof(char)*len);
char * str = new char[len];
return str;
}
int main(void)
{
char * str = MakeStrAdr(20);
strcpy(str, "I am so happy~");
cout << str << endl;
// free(str);
delete []str;
return 0;
}