cpp_const - 8BitsCoding/RobotMentor GitHub Wiki

int GetX() const;
// ν•΄λ‹Ή 책체 μ•ˆμ˜ μ–΄λ– ν•œ 것도 λ°”κΎΈμ§€ μ•Šκ² λ‹€.

const

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 <ν•¨μˆ˜λͺ…> :
<λ¦¬ν„΄ν˜•> <ν•¨μˆ˜λͺ…> 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;

μ°Έκ³ μ‚¬μ΄νŠΈ


Advanced C++ const

λ³€μˆ˜μ˜ const

λ‹€μŒμ€ 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

// 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();
   
}

getter, setter의 μ‚¬μš©μ€ κ·Έλƒ₯ λ°›μ•„λ“€μ΄λŠ”κ²Œ 정신건강에 μ’‹λ‹€.

// 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 문제일 λ“―
⚠️ **GitHub.com Fallback** ⚠️