Created
November 29, 2016 06:33
-
-
Save whoo24/4d1408eed29410f9ff29c1f2160e3bf9 to your computer and use it in GitHub Desktop.
Study of const in c++
This file contains hidden or 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
#include "stdafx.h" | |
class ClassA { | |
}; | |
class ClassB { | |
int value_ = 0; | |
int* ptr_ = nullptr; | |
ClassA member_; | |
public: | |
/* error C2440: 'return': 'const int *'에서 'int *'(으)로 변환할 수 없습니다. | |
int* Foo () const { | |
return &value_; | |
} | |
*/ | |
const int* Foo () const { | |
return &value_; | |
} | |
/* error C2440 : 'return' : 'const int'에서 'int &'(으)로 변환할 수 없습니다. | |
int& Boo () const { | |
return value_; | |
} | |
*/ | |
const int& Boo () const { | |
return value_; | |
} | |
operator int* () const { | |
return ptr_; | |
} | |
}; | |
class ClassC { | |
bool value_; | |
public: | |
const bool* value1 () const { return &value_; } | |
bool const* value2 () const { return &value_; } | |
//bool* const value3 () const { return &value_; } error C2440 : 'return' : 'const bool *'에서 'bool *const '(으)로 변환할 수 없습니다. | |
const bool* const value4 () const { return &value_; } | |
}; | |
int main() | |
{ | |
bool b = false; | |
bool* c = &b; | |
bool const* d = &b; | |
const bool* e = &b; | |
bool f = true; | |
bool* const g = &b; | |
const bool* const h = &b; | |
d = &f; | |
e = &f; | |
// g = &f; error C3892 : 'g' : const인 변수에 할당할 수 없습니다. | |
// h = &f; error C3892 : 'h' : const인 변수에 할당할 수 없습니다. | |
// *d = true; // error C3892: 'd': const인 변수에 할당할 수 없습니다. | |
// *e = true; error C3892 : 'e' : const인 변수에 할당할 수 없습니다. | |
*g = true; | |
// *h = false; error C3892 : 'h' : const인 변수에 할당할 수 없습니다. | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment