in methods: can access private members
class Tv; //선언해두어야 아래에서 사용 가능
class Remote{
friend class Tv; //서로 friend 가능
friend class Tv2; //여러 클래스와 friend 가능
public:
void set_channel(Tv&t, int c) { t.channel = c; }
void set_channel2(Tv2 & t, int c);
//t.channel 접근 가능
}
class Tv{
public:
friend class Remote; //어디에 선언되어도 상관 없음.
private:
int channel;
};
class Tv2{
public:
friend void Remote:set_channel(Tv2 &t, int c);
//friend member function사용 가능
}
inline void Remote::set_channel2(Tv2&t, int c) { t.channel = c; }
Nested Classes 클래스 내부에 클래스 선언 local definition
class Queue
{
private:
class Node
{
public: //접근도 동일하게 적용! Queue에서 Node의 private 불가
int item;
Node(int i);
};
};
Queue::Node::Node(int i) { item = i; } //:: 두번 쓰기
Exceptions
#include <cstdlib> std::abort(); call abort: stop and terminate program(Aborted(core dumped))
try catch
try{
z=func(x,y);
}
catch(const char* s){
cout << "error";
}
catch(...){} //default catch: 진짜 "..."을 사용
~
int func(int x, int y){
if(condition) throw "not allowed";
//const char* 형식 throw
//함수 실행 종료
}
try{func(x,y);}
catch(bad_func&ext){
cout <<"error";
ext.mesg();
}
...
func(int x, int y){
if(condition) throw bad_func(x,y);
}
class bad_func{
private:
int x,y;
public:
bad_func(int x_, int y_):x(x_), y(y_){}
void mesg(){ cout <<"error!!";}
};
또한 throw 아래는 전부 생략되어 memory leak: 파괴자 호출하고(delete) 끝내기 //또는 스마트 포인터 사용


<exception> class
#include <exception>
class bad_func: public std::exception
{
public:
const char* what() {return "error";}
};
catch(std::bad_func& b){}
catch(std::bad_func2& b2){}
catch(std::exception& e){}
//이걸 먼저 쓰면 bad func와 bad func2 둘다 해당!
//부모를 나중에 쓰자.
<stdexcept> class
<new> class
type cast operator (부모 클래스*)자식클래스 포인터 형변환: 가능 (자식 클래스*)부모클래스 포인터 형변환: 안전하지 못함.

dynamic_cast Type* pp = dynamic_cast<Type*>(ptr);
const_cast 포인터나 참조형의 상수성(const)를 제거하는 데에 사용 형 변환은 불가능 Type* pp = const_cast<Type*>(ptr)

만약 포인터가 가르키는 객체가 const라면 안 바뀜! 포인터의 상수성을 없애지, 객체의 상수성 없애는게 아님.
static_cast 그냥 형변환과 다를 것이 없으나, 형변환 에러를 컴파일 타임에 확인 (그냥 형변환은(c-style) 런타임 에러가 발생)
reinterpret_cast 다른 포인터간의 변환, 수와 포인터간의 변환 가능 정수를 포인터로 바꾸면 해당 주소로 직접 이동함! 위험
구조체와 포인터 간의 변환 등으로 씀
