Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
HOWTO: const and pointer variants in C and C++
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Adem, post: 3673968"] HOWTO: const and pointer variants in C and C++ : void test() { int n = 1; // n is non-const data of type int ++n; // ok const int c = 2; // c is const data of type int // ++c; // err // Case1: int* p1 = &n; // p1 is a non-const ptr to non-const data ++p1; // ok *p1 += 3; // ok // int* px = &c; // err // Case2: int* const p2 = &n; // p2 is a const ptr to non-const data // ++p2; // err *p2 += 3; // ok // Case3: const int* p3 = &c; // p3 is a non-const ptr to const data ++p3; // ok // *p3 += 3; // err // Case4: const int* const p4 = &c; // p4 is a const ptr to const data // ++p4; // err // *p4 += 3; // err // Case5: const int* const p5 = &n; // Similar to Case4 but here non-const n is treated as const data when accessed via p5 // ++p5; // err // *p5 += 3; // err // Case6: const int *const p6 = &c; // same as Case4 // ++p6; // err // *p6 += 3; // err // Case7: const int *const p7 = &n; // Same as Case5 // ++p7; // err // *p7 += 3; // err // const* int p8 = &n; // err // const* int p9 = &c; // err // const int const* pa = &n; // err: duplicate 'const' // const int const* pb = &c; // err: duplicate 'const' } For safety and speed you should use const whenever possible. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
HOWTO: const and pointer variants in C and C++
Top