일반적으로 클래스의 경우, 클래스 내부에 선언한 기능에 접근하기 위해선
범위지정연산자::를 사용했음.
static 함수들은 위의 방식으로 접근 가능.class CTest
{
public:
static void MemberFunc()
{
}
};
int main()
{
// 객체없이 호출가능
CTest::MemberFunc();
return 0;
}
사용자 지정 이름 공간
namespace MYSPACE
{
// 함수 밖에서 선언된 변수 == 전역 변수
int g_int;
}
int main()
{
g_int = 0; // 접근 불가
// 네임스페이스 안에 있으므로 이렇게 접근해야함.
MYSPACE::g_int = 0; // 가능
return 0;
}
std namespace는 c++의 표준 입출력 함수, 기능들이 담겨있는 라이브러리임.std를 만든 이유는 사용자가 std 내의 변수, 함수와 동일한 이름으로 선언할 수도 있기 때문임.int main()
{
int aa = 0;
std::cin >> aa;
int output = Test(aa);
std::cout << "aa is " << aa << endl;
std::cout << "output : " << output << endl;
return 0;
}
using 키워드를 이용해서 특정 namespace 사용을 명시하여using namespace std;
int main()
{
int aa = 0;
cin >> aa;
int output = Test(aa);
cout << "aa is " << aa << endl;
cout << "output : " << output << endl;
return 0;
}
using std::cout;
using std::cin;
using std::endl;
int main()
{
int aa = 0;
cin >> aa;
int output = Test(aa);
cout << "aa is " << aa << endl;
cout << "output : " << output << endl;
return 0;
}
cout : ostream 객체의 extern 변수cin : istream 객체의 extern 변수명iostream.h 에 접근해서 사용하는 외부 변수.빈 클래스의 크기는? : 아무것도 없으면 1 Byte를 줌
class CTest
{
}
CTest inst; //
class CMyOstream
{
public:
CMyOstream& operator << (int _data)
{
printf("%d", _data);
return *this;
}
}
wcout, wcin 2 Byte 단위로 문자열을 처리하는 객체도 있음.class CMyOstream
{
public:
CMyOstream& operator << (const wchar_t* _data)
{
wprintf(L"%s", _data);
return *this;
}
}
CMyOstream mycout;
int main()
{
mycout << L"Hello" << endl;
// 언어 설정, 한국어로 변경
setlocale(LC_ALL, "korean");
_wsetlocale(LC_ALL, "korean");
mycout << L"안녕" << endl;
return 0;
}
class CMyIstream
{
public:
CMyIstream& operator >> (wchar_t& _target)
{
wscanf(L"%s", _target);
return *this;
}
}
CMyIstream mycin;
int main()
{
wchar_t wc;
mycin >> wc;
return 0;
}
void MyEndL()
{
printf('\n');
}
class CMyOstream
{
public:
CMyOstream& operator << (const wchar_t* _data)
{
wprintf(L"%s", _data);
return *this;
}
CMyOstream& operator << (void(*func)(void))
{
func();
return *this;
}
}
새로운 기능들이 아니라 오퍼레이터 오버로딩, 함수 포인터 등으로 구현해둔 것.