<기타 공부>/[기술서적]
[PPP - Ch 8] Functions - Drills
콜리브리
2020. 12. 5. 16:33
What is the difference between a declaration and a definition?
Declaration (선언) : 여러 번 반복해서 같은 선언 가능. 메모리 차지하지 않음. (+ 함수의 경우 파라미터 이름 생략 가능)
Definition (정의) : 단 하나의 정의만 존재할 수 있음. 메모리 차지함.
int a; // Declaration
extern int b; // Declaration
extern int b; // Can be repeated
int a = 10; // Definition (Memory allocated)
void swap(int&, int&); // Declaration; argument names unnecessary
// Definition
void swap(int& a, int& b){
int temp = a;
a = b;
b = temp;
}
What is the purpose of a namespace?
a named scope nested in the global scope or in another namespace; used to avoid name clashes
How does a namespace differ from a class?
namespace : organize classes, functions, data, and types into an identifiable and named part of a program without defining a type
class : organize functions, data, and types into a type
// Fully qualified name (::)
std::cout << "Hello" << endl;
// using declaration
using std::cout;
cout << "Hello" << endl;
// using directive
using namespace std;
cout << "Hello" << endl;
cin >> s;
// Ch8 Drill 3
using namespace std; // using directive
namespace X {
int var;
void print() { cout << var << endl; }
}
namespace Y {...}
namespace Z {...}
int main()
{
X::var = 7;
X::print(); // print X's var
using namespace Y;
var = 9;
print(); // print Y's var
///
/// c++ scope with brackets
{
using Z::var;
using Z::print;
var = 11;
print(); // print Z's var
}
///
print(); // print Y's var
X::print(); // print X's vars
}
더보기
Result >
7
9
11
9
7
Pass-by-value / Pass-by-const-reference / Pass-by-reference
Pass-by-value : copy arguments into function local var
Pass-by-reference : function local var refers back to the argument; modifies outside argument (PS 시간 초과 요인)
Pass-by-const-reference : Reference 인데, const 때문에 내용 변경 불가. Copy 안된다는 이점만 있음.
// Ch8 Drill 2
void swap_v(int a, int b) { int temp; temp = a, a = b; b = temp; } // a, b are local variables that are destroyed after function returns. x, y in main is not swapped
void swap_r(int& a, int& b) { int temp; temp = a, a = b; b = temp; } // a, b are reference to local variables in main outside of the function call. x, y in main are swapped.
// void swap_cr(const int& a, const int& b) { int temp; temp = a, a = b; b = temp; } // Doesn't compile. 'const' prohibits changes to contents referred back by 'a' and 'b'
void constant_test(const int& a, const int& b) { cout << a << " " << b << endl; }
int main() {
auto swap_choose = constant_test; // function ptr
int x = 7;
int y = 9;
cout << x << " " << y << endl;
swap_choose(x, y); // replace ? by v, r, or cr
swap_choose(7, 9);
cout << x << " " << y << endl;
const int cx = 7;
const int cy = 9;
cout << cx << " " << cy << endl;
swap_choose(cx, cy);
swap_choose(7.7, 9.9);
cout << cx << " " << cy << endl;
double dx = 7.7;
double dy = 9.9;
cout << dx << " " << dy << endl;
swap_choose(dx, dy);
swap_choose(7.7, 9.9);
cout << dx << " " << dy << endl;
}
/*
swap_v : pass-by-value; argument로 들어오는 값들이 local variable a, b에 카피되고, 카피된 값인 a랑 b가 바뀌고 실제 함수 바깥의 값들은 바뀌지 않음
swap_r : pass-by-reference; a,b가 참조하는 함수 바깥의 값을 바꾸기 가능.
다만, reference 특성상 lvalue만 받을 수 있기에 swap_choose(7.7, 9.9) 같은 literal이 argument로 오는 콜은 안됨.
그리고, double → int & 자동변환 안됨.
swap_cr : pass-by-constant-reference; const 때문에 a, b가 참조하는 값을 바꿀 수 없음. swap 정의 자체가 허용이 안됨
constant_test : literal도 argument로 받기 가능.
또 여기선 double → const int & 자동변환 됨.
*/
Avoid using global variable; 어느 함수에서 어떻게 쓰이는지 파악하기 힘들고, 따라서 오류났을때 디버깅하기 매우 어렵다.