cpp_const - 8BitsCoding/RobotMentor GitHub Wiki
int GetX() const; // ν΄λΉ μ± μ²΄ μμ μ΄λ ν κ²λ λ°κΎΈμ§ μκ² λ€.
const : μμ
const <μλ£ν> <λ³μλͺ
> : λ³μλ₯Ό μμν νλ€.
<μλ£ν> const <λ³μλͺ
> : λ³μμ μ£Όμκ°μ μμν νλ€.
const double PI = 3.14;
PI = 3.14; // Compile Error
int n = 10;
const int* pN = &n;
*pN = 20; // Compile Error
int n1 = 10;
int n2 = 20;
int* const pN = &n1;
// μ£Όμκ°μ constν νλ€.
*pN = 20;
pN = &n2; // Compile Error
// λ©€λ²λ³μμ μμν
class Student {
const int id;
...
public:
Student(int _id, int _age, char* _name) {
id = _id; // Compile Error
μμ λ¬Έμ λ₯Ό ν΄κ²¨ν λ°©μμ??
// λ©€λ²λ³μμ μμν
class Student {
const int id;
...
public:
Student(int _id, int _age, char* _name) : id(_id), age(_age) {
// λ€μκ³Ό κ°μ λ°©λ²μΌλ‘ ν΄κ²°κ°λ₯
const <리ν΄ν> <ν¨μλͺ > : 리ν΄μ μμν
<리ν΄ν> const <ν¨μλͺ > :
<리ν΄ν> <ν¨μλͺ > const : λ©€λ² ν¨μμ μμν(μλμ°Έκ³ )
- ν¨μ λ΄λΆμμ λ©€λ²λ³μμ κ°μ λ³κ²½ν μ μμ
- μ΄ ν¨μ λ΄μμ μμν λμ§ μμ ν¨μμ νΈμΆμ νμ©νμ§ μμ
- λ©€λ²λ³μμ ν¬μΈν°μ 리ν΄μ νμ©νμ§ μμ.
void ShowData() const { // κ²°κ΅ μ€μν건 λ΄λΆμμ μμ μ κΈνλ€. + μμμ΄μΈμ μ¬μ©μ κΈνλ€. cout << "μ΄λ¦: " << name << endl; cout << "λμ΄: " << age << endl; }
// Example 1
class Count
{
int cnt;
public:
Count() : cnt(0) {}
int* GetPtr() const {
return &cnt; // Compile Error
// const λ©€λ²κ° μλ λ©€λ²λ₯Ό μ¬μ©ν μ μλ€.
}
void ShowData() const {
ShowIntro(); // Compile Error
// μμν λμ§ μμ ν¨μλ₯Ό νΈμΆ ν μ μλ€.
void ShowIntro() {
cout << "νμ¬ countμ κ° :" << endl;
μ΄λ»κ² ν΄κ²°νλ?
class Count
{
int cnt;
public:
Count() : cnt(0) {}
const int* GetPtr() const {
return &cnt; // μμλ λ¦¬ν΄ κ°λ₯νλ€.
}
void ShowData() const {
ShowIntro(); // μμ ν¨μλ νΈμΆμ΄ κ°λ₯νλ€.
void ShowIntro() const {
cout << "νμ¬ countμ κ° :" << endl;
λ€μμ C++μ κ·μΉμ΄κΈ°μ μ κ·Έλ°μ§ κ³ λ―Όν νμκ° μμ λ°μ λ€μΌ κ²
int i = 1;
const int *p1 = &i;
// data(i)λ const
// pointer(p1)μ value
int * const p2 = &i;
// data(i)λ value
// pointer(p1)μ const
const int * const p3 = &i;
// data(i)λ const
// pointer(p1)μ const
int const * p4 = &i;
// data(i)λ const
// pointer(p1)μ value
μ’ λ μ½κ² μΈμ°κΈ° μν΄μλ * μΌμͺ½μ constκ° μμ μ dataκ° const
- μ€λ₯Έμͺ½μ constκ° μμ μ pointerκ° const
// const used with functions
class Dog {
int age;
string name;
public:
Dog() { age = 3; name = "dummy"; }
// const parameters
// ν¨μ λ΄λΆμμ λ§€κ°λ³μλ₯Ό λ³κ²½νμ§ μμμ μλ¦Ό
void setAge(const int& a) { age = a; }
// μ΄λ κ²λ μ°μ§λ§μ
// void setAge(int& a) { age = a; }
// Const return value
const string& getName() {return name;}
// const function
void printDogName() const { cout << name << "const" << endl; }
void printDogName() { cout << getName() << " non-const" << endl; }
};
int main() {
Dog d;
d.printDogName();
const Dog d2;
d2.printDogName();
}
// setter
void setAge(const int& a) { age = a; }
// μ¬μ©νκΈ°
object.setAge(77); // κ·Έλ₯ λ°Έλ₯λ₯Ό λ£μΌλ©΄ λ¨.
// getter
const string& getName() {return name;}
// μ¬μ©νκΈ°
string name;
string& c_name;
name = object.getName();
c_name = object.getName();
// μ°Έμ‘°νμΌλ‘ λ°μλ μ’κ³ λ°Έλ₯λ‘ λ°μλ 볡μ¬κ° λ°μ.
// μ°Έμ‘°νμ μμ λΆκ°
// κ°μ μ
λ ₯ λ°μ λ³κ²½νκ³ μΆλ€λ©΄??
// μλμ κ°μ΄ νλ©΄λλ€.
int * Example(int * ptr){
return ptr+1;
}
// νμ§λ§... μμ κ°μ κ²½μ°λ§ μλ κ²μ΄ μλλ€.
// κ°μ λ°μμ λ³κ²½ν μμ§κ° μλ€λκ±Έ μ½λμμ νννκ³ μΆλ€λ©΄?(Ex> setter)
void Example(const int &ref){
//λ΄μ©λ΄μ©λ΄μ©
}
// μμ κ°μ΄ const int &λ₯Ό λΆμ¬ μ€μΌλ‘μ ν΄λΉ ν¨μμμ κ°μ λ³κ²½νμ§ μμμ μλ €μ€ μ μλ€.
// Cf>
// Constλ₯Ό μμ λ²λ¦΄ μ ν¨μ λ΄λΆμμ λ³κ²½μ΄ κ°λ₯νλ€.
void Example(int &ref){
//λ΄μ©λ΄μ©λ΄μ©
}
μ°Έμ‘°ν 리ν΄ν¨μκ° μλμ κ°μ΄ λ κ°κ° μλ€κ³ νμ.
int& FuncOne(int &a)
{
a++;
return a;
}
int FuncTwo(int &a)
{
a++;
return a;
}
FuncOne : μ°Έμ‘°νμ 리ν΄
FuncTwo : λ³μλ₯Ό 리ν΄
int num1;
int num2 = FuncOne(num1);
// num2λ FuncOneμμ 리ν΄ν΄μ£Όλ λ³μλ₯Ό 볡μ¬νλ€.
// num1, num2λ λ€λ₯Έ λ³μκ° λλ€.
int num1;
int &num2 = FuncOne(num1);
// num1κ³Ό num2λ κ°μ λ³μκ° λλ€. (μ°Έμ‘°νμ 리ν΄ν΄μ λ°κΈ°μ)
int num1;
int num2 = FuncTwo(num1);
// λ³μ 볡μ¬κ° λλ©° μμ num1, num2λ λ€λ₯Έ λ³μ
int num1;
int &num2 = FuncTwo(num1);
// μ»΄νμΌ μλ¬κ° λ°μνλ€.
// κ·Έλ°λ° int& num2 = num1μ΄λ°κ±΄ λμμ?
// νμ§λ§ μλ¨ μλ§ r, l value λ¬Έμ μΌ λ―