|
| mcleano (535) | |
Hey, I was wondering if someone could write an article on const correctness and all the uses of const, with what they do. I think this would be a good addition to the article collection and I know i would find it extremely useful.Any takers? | |
| jsmith (3099) | |
| Here's a start. It documents what the keyword "const" means in various places within a declaration. It does not go into the reasons why const-correctness is important. I wrote this several years ago. The following declarations are identical: const char* p; char const* p; Both declare a pointer to a constant character. The second is slightly better in the sense that the declaration can be read from right-to-left: "p is a pointer to a const char". Read as such, it is easy to see that the line *p = 'c'; will not compile. The following declaration: char* const p; declares p to be a constant pointer to a character. That is: p = "foo"; // Does not compile *p = 'f'; // Compiles! And thus: const char* const p; char const* const p; both declare p to be a constant pointer to a constant character, and so none of the following lines of code compile: p = "foo"; *p = 'f'; Now throw another pointer into the mix: const char** p; char const** p; These are equivalent and declare p to be a pointer to a pointer to a constant character. That is: p = ptr-to-ptr-to-char; // Compiles *p = ptr-to-char; // Compiles **p = 'f'; // Does not compile Or how about creative placement of const: char* const* p; This declares p to be a pointer to a constant pointer to a character. That is: p = ptr-to-constptr-to-char; // Compiles *p = ptr-to-char; // Does not compile *p = constptr-to-char; // Does not compile **p = 'f'; // Compiles And the ever-popular: char** const p; Which declares p to be a constant pointer to a pointer to a character. Or: p = ptr-to-ptr-to-char; // Does not compile p = constptr-to-ptr-to-char; // Does not compile *p = ptr-to-char; // Compiles **p = 'f'; // Compiles And now we get just plain const happy: const char* const* p; p is a pointer to a constant pointer to a constant character. The only thing you can do with this one (besides remove the code and rewrite) is: p = ptr-to-constptr-to-constchar; const char** const p; p is a constant pointer to a pointer to a constant character. The only thing you can do with this is: *p = ptr-to-constchar; And this beast: const char* const* const p; Well, it won't pass code review since nobody will understand it, but at any rate... We've achieved maximum constant-ness with this line. You can't do anything at all with p, what it points to, what that points to, or what "what that" points to. You can print it. That's about it. Ho-ho, and the fun is just beginning. Now throw in REFERENCES! const char& p; char const& p; These both declare p to be a reference to a constant character. That is, p cannot change. char& const p; const char& const p; char const& const p; char*& const p; const char*& const p; const char* const& const p; These all generate compiler errors, because there is no such thing as a constant reference I guess. const char*& p; char const*& p; p is a reference to a pointer to a constant character. One can change p, but not *p. char* const& p; p is a reference to a constant pointer to a character. const char* const& p; p is a reference to a constant pointer to a constant character. const char&* p; char const&* p; char& const* p; char&* const p; const char& const* p; const char&* const p; const char& const* const p; Fortunately pointers to references are not allowed. The above declarations are illegal. | |
| guestgulkan (1040) | |||
| |||
Last edited on | |||
| guestgulkan (1040) | |
| I think mcleano should do some research and write the article. | |
Last edited on | |
| mcleano (535) | ||
You know what, I might just do that. | ||
| kevinchkin (363) | |
| A very good article on constant correctness http://www.possibility.com/Cpp/const.html | |
| mcleano (535) | |
| Thanks, but as guestgulkan suggested, I think I might give this a try. | |
Registered users can post in this forum.
