On Sep 20, 9:15 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
In my function/method paramater list I try to keep constant correctness.
I.E. I would use:
int Foo( const char* a, int b )
instead of
int Foo( char* a, int b )
My question reguards int b. Would it be more correct to make it
int Foo( const char* a, const int b )
instead? I do know the effect of this would be that I can't change b in the
function, which I normally never do anyway.
Can anyone think of a compelling reason to use one over the
other? Or is it just a matter of style?
It's largely a matter of style. As Craig Scott pointed out, top
level const is ignored in the declaration. And since the
argument is passed by value, you have a local copy; whether you
modify it or not is irrelevant to the client, since he won't see
any of the modifications anyway.
I think most programmers just ignore const completely here. For
those who want the const in the definition (where it is
significant, and can provide information to whoever reads the
function body), there are two schools of thought:
-- only use it in the definition, since it is irrelevant (and
an implementation detail, which may change) to the user, or
-- use it in the declarations as well, so that the declarations
and the definitions are identical.
(I rather prefer the first, but in practice, like most people I
know, I don't use const here.)
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34