C++中如何正确使用const

来源:互联网 发布:揭阳华诺网络 编辑:程序博客网 时间:2024/05/19 10:08

这篇博客将会介绍在C++中如何正确使用const。


先摘录一段wikipedia中的代码:

void Foo( int * ptr,          int const * ptrToConst,          int * const constPtr,          int const * const constPtrToConst ){    *ptr = 0; // OK: modifies the "pointee" data    ptr  = NULL; // OK: modifies the pointer     *ptrToConst = 0; // Error! Cannot modify the "pointee" data    ptrToConst  = NULL; // OK: modifies the pointer     *constPtr = 0; // OK: modifies the "pointee" data    constPtr  = NULL; // Error! Cannot modify the pointer     *constPtrToConst = 0; // Error! Cannot modify the "pointee" data    constPtrToConst  = NULL; // Error! Cannot modify the pointer}

0 0
原创粉丝点击