728x90
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// If the object is const, both the function and the variable become read-only. | |
/// (Korean) 객체가 const 인 경우, 함수와 변수 모두 읽기 전용 값이 된다. | |
/// | |
class ConstTest | |
{ | |
public: | |
ConstTest() { a = 10; } | |
int a; | |
const int b = 1000; | |
public: | |
int get_a_const() const { return a; } | |
int get_a() { return a; } | |
int get_b() { return b; } | |
}; | |
void test( const ConstTest* pConst, ConstTest* pNonConst ) | |
{ | |
int ret1 = pConst->get_a_const(); /// OK | |
// int ret2 = pConst->get_a(); /// COMPILE ERROR | |
int ret3 = pNonConst->get_a_const(); /// OK | |
int ret4 = pNonConst->get_a(); /// OK | |
} | |
int main(int argc, char* argv[]) | |
{ | |
ConstTest ct1, ct2; | |
test( &ct1, &ct2 ); | |
const ConstTest ct3; | |
ct3.get_a_const(); /// OK | |
// ct3.get_a(); /// COMPILE ERROR | |
ConstTest ct4; | |
ct4.get_b(); /// OK | |
// ct4.b = 300; /// COMPILE ERROR | |
const ConstTest ct5; | |
// ct5.a = 100; /// COMPILE ERROR | |
return 0; | |
} |
728x90
반응형
'C C++' 카테고리의 다른 글
리눅스 환경에서 tzset()와 fork() 간의 잠금 문제 분석 및 해결 방안 (0) | 2011.07.12 |
---|---|
QtConcurrent : MapReduce 모델에 대한 Qt의 대안 (0) | 2011.01.08 |
c++ std exception example (0) | 2010.05.04 |
[CodeBlocks & wxPack] 손쉽게 Windows에서 wxWidgets(wxWindows) 개발 환경 구성하기 (3) | 2009.10.30 |
Google ctemplate (0) | 2009.07.08 |