473,790 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parametre de fonction const

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
Jul 23 '05 #1
3 5233
"Philippe Mesmeur" <ph************ **@noos.fr> wrote in message
news:42******** *************** @nan-newsreader-06.noos.net...
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


I think the best style is this:

// somefile.h
void func(int x);

// somefile.cpp
void func(const int x) {...} // const being optional

Whether or not x is modified in the .cpp file is an implementation detail
which does not and indeed should not appear at the interface (the .h file).

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #2
Philippe Mesmeur wrote:
[redacted]
Philippe


You know, I bet a lot more people would help you if you posted in
English....
Jul 23 '05 #3
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.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16247
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
6
2424
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk implicitly. The destructor of this class saves the object to the disk if it is dirty. The problem comes in the following scenario when a function returns an uncommitted pointer class because same copies will be committed as two separate objects on disk....
15
4191
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef MY_HEADER #define MY_HEADER const int FOO = 42;
3
2843
by: Chris | last post by:
function Main(param) { alert("test "+param); <<<<<<<<<<< Ici tout se passe bien : le contenu de param est bien affiché fchaine='' .... +'<div ><a href="#" onclick="javascript:return Suite('+param+');"><IMG src="tg.gif" ></a></div>' .... document.write(fchaine); }
4
2751
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
6
1464
by: aliassaf | last post by:
Hello, If we write = x^2 and if I give to the program the values of x, it will going to calculate the values of y, and also for x. But it is possible ? that is if I give to the program the values of X and Y, it will indicate to me the relation between the two variables, in the other hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me that f (t)!!!
10
2794
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1876
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
4
6697
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.