728x90
반응형
///
/// 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
반응형

+ Recent posts