回 帖 发 新 帖 刷新版面

主题:[原创][C++提高]指针、引用与const 连用

一些C++构造很容易使人糊涂。其中之一便是指针、引用与const 连用。有时引用
的内容、指针甚至两者是否都是常量,很不清楚。下例可以帮助弄清事实:

/******* pointers to integers *******/

//declare some data
const int nine = 9;
int number;

//the pointer is constant
int * const n1 = &number;

//the value pointed at is constant
const int * n2 = &nine;

//both are constant
const int * const n3 = &nine;

/******* pointers to characters *******/

//the text is constant
const char *s1 = {"text"};

//the pointer is constant
char * const s2 = {"text"};

//both are constant
const char * const s3 = {"text"};

/******* arrays of pointers to characters *******/

//the characters are constant
const char * text1[] = {"line1","line2","line3"};

//the pointers are constant
char * const text2[] = {"line1","line2","line3"};

//both are constant
const char * const text3[] = {"line1","line2","line3"};

回复列表 (共2个回复)

沙发

[em1]
非常感谢啊!!

板凳

哈哈,客气!

我来回复

您尚未登录,请登录后再回复。点此登录或注册