Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const parameters

Reply
Thread Tools

const parameters

 
 
gc
Guest
Posts: n/a
 
      11-07-2003
I came across certain code with lot of declarations like:
int foo(const double bar,const int m);
I do not see any point in making a non pointer parameter a const, or
am I missing something?
 
Reply With Quote
 
 
 
 
Anupam
Guest
Posts: n/a
 
      11-07-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (gc) wrote in message news:<(E-Mail Removed). com>...
> I came across certain code with lot of declarations like:
> int foo(const double bar,const int m);
> I do not see any point in making a non pointer parameter a const, or
> am I missing something?

Why not? I would say that it is definitely to the point if you can
say to the compiler that you will *not* be changing bar or m inside
the function foo. Ok, this value will not propagate to the caller,
which is why making a pointer, a constant is a good idea. However if
you are sure that your function should not manipulate these values
inside the function, its a good idea to give yourself a compile time
escape route.
For instance :

<code excerpt>
void foo(int bar[],const int noElements) {
unsigned int i;
for(i=0;i<noElements;i++) }
/* blah blah... whatever you want to do with bar[i] */
/* What would happen if you inadvertently put a noElements--
around here */
}
}

It is a good idea to make a non pointer parameter const for this
reason.
Regards,
Anupam
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      11-07-2003
"gc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I came across certain code with lot of declarations like:
> int foo(const double bar,const int m);
> I do not see any point in making a non pointer parameter a const, or
> am I missing something?


In the function's implementation, it makes just as much sense
as making any other local variable 'const'.

However, the 'const' is irrelevant in the interface of a function.
And the compiler knows this: the following declaration and definition
are recognized by the compiler as referring to the same function:

// file.h
int foo(double bar,int m);

// file.cpp
int foo(const double bar,const int m);


This is the style I typically use - keep the details away from
the user's eyes (in the header), but use const whenever it can
help code maintenance...


hth - Ivan
--
http://ivan.vecerina.com


 
Reply With Quote
 
rihad
Guest
Posts: n/a
 
      11-07-2003
On 6 Nov 2003 19:15:12 -0800, (E-Mail Removed) (gc) wrote:

>I came across certain code with lot of declarations like:
>int foo(const double bar,const int m);
>I do not see any point in making a non pointer parameter a const, or
>am I missing something?


It just means that the implementor of foo() cannot modify their copy of passed
parameters `bar' and `m' inside foo(). It means nothing to the user of foo().
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-07-2003
gc <(E-Mail Removed)> spoke thus:

> I came across certain code with lot of declarations like:
> int foo(const double bar,const int m);


It's a way of reducing the number of errors you produce. Parameters are
passed by value, so changing them never affects anything outside the function.
Declaring parameters const is a way of forcing yourself to remember this.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      11-07-2003
rihad <(E-Mail Removed)> writes:

> On 6 Nov 2003 19:15:12 -0800, (E-Mail Removed) (gc) wrote:
>
> >int foo(const double bar,const int m);
> >I do not see any point in making a non pointer parameter a const, or
> >am I missing something?

>
> It just means that the implementor of foo() cannot modify their copy of passed
> parameters `bar' and `m' inside foo().


It doesn't even mean that, because the function's implementation is
free to omit the const qualifiers regardless of whether they are
included in this prototype.

> It means nothing to the user of foo().


--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments