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 의 특성 내용 은 예전 의 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Visual Studio에서 파일 폴더 구분 (포함 경로 설정)Visual Studio에서 c, cpp, h, hpp 파일을 폴더로 나누고 싶었습니까? 어쩌면 대부분의 사람들이 있다고 생각합니다. 처음에 파일이 만들어지는 장소는 프로젝트 파일 등과 같은 장소에 있기 때문에 파일...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.