C++에서 const 의 특성 사용

5710 단어 C++const
디렉토리(역할):
1:변 수 를 수식 하 는 것 은 이 변 수 를 바 꿀 수 없다 는 것 을 의미한다.
2:수식 지침 은 상수 만 생각 하 는 지침 과 자신 이 상수 인 지침 으로 나 뉜 다.
3:수식 인용 은 상수 의 인용 을 가리 키 며 변형 인삼 을 수식 하 는 데 사 용 됩 니 다.즉,복사 가 피 할 수 있 고 함수 가 값 에 대한 수정 을 피 할 수 있 습 니 다. 
4:구성원 함수 수정:이 구성원 함수 에서 구성원 변 수 를 수정 할 수 없다 는 것 을 설명 합 니 다.
5:포인터 와 인용
본문:
다음은 각종 상황 에 대한 예 이다.

// :1:const     cj                ,     i     
#include <iostream>

using namespace std;

int main() {
  int i = 1;
  const int &cj = i;
  
  cout << "cj : " <<cj<< endl;(√)
  
  
  i=2;
  cout << "cj : " <<cj<< endl;(√)
  
  cj=3;
  cout << "cj : " <<cj<< endl;(×)
  
  int a=9;
  cj=a; (×)
  
  return 0;
}



    :
/code/main.cpp: In function ‘int main()':
/code/main.cpp:15:4: error: assignment of read-only reference ‘cj'
cj=3;
^
/code/main.cpp:19:4: error: assignment of read-only reference ‘cj'
cj=a;
^
sandbox> exited with status 0

// :    ,         :

#include <iostream>

using namespace std;

int main() {
  const int i = 4;
 
  const int &ck = i; //  ,        const  
   cout<< "ck="<<ck<<endl;
  
  const int b = 5;
 
  int &r = b;  //  ,
  
  return 0;
}



/code/main.cpp: In function ‘int main()':
/code/main.cpp:13:14: error: invalid initialization of reference of type ‘int&' from expression of type ‘const int'
  int &r = b;  //  ,
       ^
sandbox> exited with status 0

// :const      :

#include <iostream>

using namespace std;

int main() {
  double b = 2.14;
  const int &a = b;
  //        :
//   int temp = b;
//   const int &a=temp;
  //   , b    ,a  
  cout<<"a="<<a<<endl;
  return 0;
}

    :
a=2
sandbox> exited with status 0

// :      _1:

class Date
{
  private:
  int m_year;
  int m_month;
  int m_day;
  public:
  int GetDay(void) const
  {
    m_day=7;
    return m_day;//      ,           ;
  }
};

// void GetDay(void) const
// {
//   return m_day;

// }

int main() {
  double b = 2.14;
  const int &a = b;
  //        :
  //   int temp = b;
  //   const int &a=temp;
  //   , b    ,a  
  cout<<"a="<<a<<endl;
  return 0;
}


    :
/code/main.cpp: In member function ‘int Date::GetDay() const':
/code/main.cpp:16:8: error: assignment of member ‘Date::m_day' in read-only object
 m_day=7;
    ^
sandbox> exited with status 0

// :    _2

#include <iostream>

  using namespace std;



class Date
{
  private:
  int m_year;
  int m_month;
   mutable int m_day;//   mutable       ,       
  public:
  int GetDay(void) const
  {
    m_day=7;
    return m_day;
  }
};

// void GetDay(void) const
// {
//   return m_day;

// }

int main() {
  double b = 2.14;
  const int &a = b;
  //        :
  //   int temp = b;
  //   const int &a=temp;
  //   , b    ,a  
  cout<<"a="<<a<<endl;
  return 0;
}


    :
a=2
sandbox> exited with status 0

// :const     


#include <iostream>

  using namespace std;


int main() {
  const int* p = NULL;//       *p    
  //int const* p = NULL;

  int a=9;
  p=&a;//   p     ,      
  cout<<"*p="<<*p<<endl<<"p="<<p<<endl;
  
  
  int c=10;
  int* const b = &c;//       p     
   c=45;
  *b=c;//   b    ,      
  cout<<"*b="<<*b<<endl<<"b="<<b<<endl;
  
  b=&a;//      ,b           
  cout<<"*b="<<*b<<endl<<"b="<<b<<endl;
  return 0;
}

    :
/code/main.cpp: In function ‘int main()':
/code/main.cpp:21:3: error: assignment of read-only variable ‘b'
 b=&a;
  ^
sandbox> exited with status 0

// :const     

#include <iostream>

  using namespace std;


int main() {
  int x = 3;
  const int& y = x;
  cout<<"y="<<y<<endl;
  x=9;
  cout<<"y="<<y<<endl;
  
  y=9;//const                    
  cout<<"y="<<y<<endl;
  return 0;
}


    :
/code/main.cpp: In function ‘int main()':
/code/main.cpp:13:3: error: assignment of read-only reference ‘y'
 y=9;
  ^
sandbox> exited with status 0
C++에서 const 의 특성 에 대한 사용 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 C+const 의 특성 내용 은 예전 의 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기