Philippe Mesmeur wrote:
J'ai eu une longue discussion hier au sujet des parametres de fonction
const.
La personne etait pour mettre "const" devant TOUS les parametres ne devant
pas etre modifies.
A mon avis, il faut le faire que SI le parametre est un pointeur ou une
référence.
int fct1(const int* i);
int fct2(const int& i);
La discussion tournait autours d'une utilisation du const comme:
int fct3(const int i);
comme un argument est passé par copie à une fonction, meme si à la sortie la
copie à été modifiée, il n'en sera rien pour la valeur initiale.
Je pense donc que le const n'a pas d'interet.
D'autres disent que le fait de mettre un const empeche de modifier la valeur
du parametre dans le code de la fonction (ce qui n'est pas faut) et qu'il
faut mettre const des que possible (c'est à dire tres souvent).
Que pensez vous de tout ça?
Connaissez vous de bonne pages www qui traitent du sujet.
Merci d'avance pour la réponse
Philippe
According to Babel Fish Translation
http://babelfish.altavista.com/tr
I had a long discussion yesterday
about the parameters of function const.
The person etait to put "const"
in front of ALL the parameters not having to be modify.
With my opinion, it should it be made that
IF the parameter is a pointer or a reference.
int fct1(const int * i);
int fct2(const int& i);
The discussion turned goshawks of a use of the const like:
int fct3(const int i);
as an argument passed by copy to a function,
same if at the exit the copy at summer modified,
it of it will be nothing for the initial value.
I thus think that the const does not have interest.
Others say that the fact of putting a const empeche
to modify the value of the parameter in the code of the function
(what is necessary) and that is not should be put const
as soon as possible (i.e. very often).
What think of all that?
Know of good www pages which cover subject.
Thank you in advance for the answer
Philippe
The *declaration*
int fct3(const int i);
is equivalent to the *declaration*
int fct3(int i);
but const qualifier may be useful in the *definition*
int fct3(const int i) {
i = 33; // error
// other code
return i + 1;
}
where the argument i is a constant that should *not*
be changed in the body of function definition.