指针常量和指向常量的指针
#include <iostream>
using namespace std;
int main()
{
int i=10;
int j=20;
int * const cp = &i;
cout<<*cp<<endl;
(*cp)++; //ok: Can modify the variable pointed
cout<<*cp<<endl;
//cp = &j; //error: Canot modify a const object
const int * pc ;
pc = &i;
cout<<*pc<<endl;
//(*pc)++; //error: Cannot modify a const object
pc = &j;
cout<<*pc<<endl;
//const int * const cpc; // Constant variable 'cpc' must be initialized
const int * const cpc = &j;
cout<<*cpc<<endl;
(*cpc)++; //Cannot modify a const object
cpc = &j; //Cannot modify a const object
return 0;
}
最近喜欢上了Emacs 24.2.1 + BCC5.5写C++程序.

浙公网安备 33010602011771号