首页 > 代码库 > const

const

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
const int N=100;
int const N=100; //二者等价
int mark=0;
//1
int* ref_mark=&mark;
int* const book1=ref_mark;//指针book1是个常量,并没有说明这个指针指向的int值是个常量
const int* book2=ref_mark;//指针book2是个指针类型的常量
cout<<"N1:"<<N1<<endl;
cout<<"N2:"<<N2<<endl;
    *book1=10;
cout<<*book1<<endl;
*book2=20;
/*
|error: assignment of read-only location ‘* book2’|
const int *book2=ref_mark
*/
cout<<*book1<<endl;
cout<<*book2<<endl;
    //2
typedef char* CHARS;

typedef CHARS const CPTR;
//替换后
typedef char * const CPTR;
//仍然是指向char类型的常量指针

typedef const CHARS CPTR;
//替换后
typedef const char * CPTR;
//是指向char类型的指针

}