A
alf.p.steinbach
good for who?
"whom"
I.e., "good for whom?".
You can also ask, "who is it good for?".
For more info, see <url: http://www.elearnenglishlanguage.com/difficulties/whowhom.html>.
That said, goodness of code is sort of like entropy, in that the more accurately you try to pin it down, the more fuzzy and ill-defined it gets, BUT ordinarily the difference between much of it and none of it is so exceedingly great that the fine details don't matter.
I.e., to a first approximation, good code is good for everybody. ;-)
[snip]
i not agree, i consider all memory accessible read-write
and if there are mem place where i have to write only one time
i write it... but i never have problem about this
for example the string
"%s\n"
in the format of
printf("%s\n", a)
is conceptually a const string
for the compiler a const string
for the machine a read write string
but even if i possibly can write on that
never happen it
so i think all your tell of how usefull is of const keyword
is void
in pratical case
A practical motivation for using "const" is just that the tools support andenforce it.
For example,
Code:
#include <iostream>
using namespace std;
int main()
{
char* p = "Blah blah blah, const is so overrated.";
p[0] = 'b';
cout << p << endl;
}
Compiling and running with Visual C++ 11.0 (Visual C++ 2012):
[example]
[D:\dev\test]
CL=/EHsc /GR /FI"iso646.h" /we4627 /we4927 /W4 /D"_CRT_SECURE_NO_WARNINGS" /nologoset cl
[D:\dev\test]
cl foo.cpp foo.cpp
[D:\dev\test]
foo
-- A crash dialog pops up, http://imgur.com/bpZDHIe
[D:\dev\test]
[/code]echo %errorlevel% -1073741819
[D:\dev\test]
The process exit code reported by the ERRORLEVEL pseudo-variable happens tobe Windows error code 5 translated to an "NT status code" (what you have in Windows structured exceptions), 0x0xC0000005, where code 5 is "Access is denied" with symbolic constant ERROR_ACCESS_DENIED in <winerror.h>.
So the effect was as-if it that literal string were read only, which it probably was. ;-)
With "const" you would have got a compilation error instead.
For a larger program a compilation error (via "const") would save you a lotof systematic testing of runtime behavior, and associated bug hunting.
Cheers & hth.,
- Alf