473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

const parameters

gc
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?
Nov 13 '05 #1
5 9295
gu**********@yahoo.com (gc) wrote in message news:<79**************************@posting.google. 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
Nov 13 '05 #2
"gc" <gu**********@yahoo.com> wrote in message
news:79**************************@posting.google.c 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
Nov 13 '05 #3
On 6 Nov 2003 19:15:12 -0800, gu**********@yahoo.com (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().
Nov 13 '05 #4
gc <gu**********@yahoo.com> 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.
Nov 13 '05 #5
rihad <ri***@mail.ru> writes:
On 6 Nov 2003 19:15:12 -0800, gu**********@yahoo.com (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
Nov 13 '05 #6

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

Similar topics

31
by: Ben | last post by:
For many times, I've found myself changing my member variables from const back to non-const. No matter how good the original objective was, there was always at least one reason not to use const...
5
by: Kenneth Massey | last post by:
I have run into a peculiar problem, in which the following sample code does not compile (gcc 3.3). I have a template class with a member function that should take a const version of the template...
7
by: Richard Cavell | last post by:
Hi, The point of using const on a parameter to a function should be to let your compiler know that the parameter shouldn't be modified during your program. This allows you to keep your code...
3
by: Ed Morton | last post by:
Anyone know why "const" isn't the default for function parameters? I almost never see code where function parameters are modified intentionally (and when I do, I usually think copying the value to...
1
by: | last post by:
Hey, all. I need some help to figure out what is the correct way in C# to pass parameters by reference but keep them constant. In C++ it's recommended to pass all of your objects by reference...
0
by: Marcelo | last post by:
Hi, I have a long value, and I have an API in which I have to pass a "const CURRENCY &Vlr". Is there any way of doing this conversion, so I can pass this long value to it? TIA Marcelo
3
by: news.microsoft.com | last post by:
It seems to me that C# has a serious issue regarding const. It's not possible to have a method parameter declared as const. Often you will want to call a method, and you don't want the method to...
3
by: Sven Groot | last post by:
This was posted by someone in comp.lang.c++, and later in microsoft.public.vstudio.general, but since I know Carl is in this group, and he's the one that should read this, I've reposted it here....
11
by: u.int.32.t | last post by:
Is there a way to make a single template function that will accept both const and non-const parameter types by reference without making a copy? For example, I can write this which will accept...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.