读书人

char *const, char const* const char

发布时间: 2012-11-26 11:48:50 作者: rapoo

char *const, char const*, const char*

5.4.1 Pointers and Constants [ptr.pc]

When using a pointer, two objects are involved: thepointer itself and the object pointed to. ‘‘Pre-fixing’’ a declaration of apointer withconst makes the object, but not the pointer, a constant. To declarea pointer itself, rather than the object pointed to, to be a constant, we usethe declarator operator*const instead of plain*.Forexample:

void f4(){       int a = 1;       const intc = 2;       const int*p1 = &c;      // ok       const int*p2 = &a;      // ok       int* p3 =&c;        // error: initialization ofint* with const int*       *p3 = 7;               // try to change the value of c}

It is possible to explicitly remove the restrictionson a pointer to const by explicit type conversion (§10.2.7.1and §15.4.2.1)


Reference:

The.C++.Programming.Language.3rd.Edition

http://www.cnblogs.com/Flouse/archive/2008/08/14/constPointer.html

读书人网 >编程

热点推荐