J
jl_post
Hi,
I'm a big fan of the "const" keyword for several reasons. I don't
want to get too deep in my reasons, but one place I'll use them is in
code like this:
const int numToUse = getSomeNumber(...);
Since I declared numToUse as const, it's clear to the maintainer that
numToUse will not change for the rest of its scope, nor is it supposed
to change (preventing a maintainer from accidentally changing it).
At any rate, sometimes I create a function/method that has a
signature like the following:
void f(int arg1);
But for my example, arg1 is essentially a constant, since it never
changes. So I COULD declare f() as:
void f(const int arg1);
but that const is basically unneeded for the maintainer who only needs
to know the signature of f(). However, putting the const keyword in
tells whoever looks at the definition of f() that arg1 is never
supposed to change.
(Note that I am not passing by reference -- only by value.)
So the advantage of putting const in is that the maintainer who
reads the definition of f() can tell that arg1 is never supposed to
change in f(). But this information is unimportant to the maintainer
who treats f() as a "black-box" -- to him/her it makes no difference
if arg1 (which is passed by value) is changed in f(), so seeing arg1
declared as const is of no help at all.
Playing around with this, I discovered that I can declare arg1 as
const in the definition, like this:
void f(const int arg1) { ... }
while not declaring arg1 as const in the declaration/signature/
prototype, like this:
void f(int arg1);
The compiler will compile this just fine, despite that "const" is not
in both lines. (I tried this on Visual C++, g++ on Win32, and g++ on
Linux.)
If you'd like to see a full working program that illustrates what
I'm talking about, here is one:
#include <iostream>
void f(int arg1);
int main(int argc, char ** argv)
{
f(77);
return 0;
}
void f(const int arg1)
{
std::cout << "arg1 = " << arg1 << std::endl;
}
So my question is: Is this an acceptable thing to do (as far as the
C++ language specifications are concerned)?
(Just because it works on all compilers I've tried it on so far
doesn't necessarily mean it's allowed in the standard language, so I'd
like to know for sure if it's a legal thing to do.)
Being able to do this is good in that the maintainers of the
function can see what passed-in arguments are never supposed to
change, while the other maintainers can treat the function as a black-
box and not need to see which passed-in-by-value arguments are const.
Thanks.
-- Jean-Luc
I'm a big fan of the "const" keyword for several reasons. I don't
want to get too deep in my reasons, but one place I'll use them is in
code like this:
const int numToUse = getSomeNumber(...);
Since I declared numToUse as const, it's clear to the maintainer that
numToUse will not change for the rest of its scope, nor is it supposed
to change (preventing a maintainer from accidentally changing it).
At any rate, sometimes I create a function/method that has a
signature like the following:
void f(int arg1);
But for my example, arg1 is essentially a constant, since it never
changes. So I COULD declare f() as:
void f(const int arg1);
but that const is basically unneeded for the maintainer who only needs
to know the signature of f(). However, putting the const keyword in
tells whoever looks at the definition of f() that arg1 is never
supposed to change.
(Note that I am not passing by reference -- only by value.)
So the advantage of putting const in is that the maintainer who
reads the definition of f() can tell that arg1 is never supposed to
change in f(). But this information is unimportant to the maintainer
who treats f() as a "black-box" -- to him/her it makes no difference
if arg1 (which is passed by value) is changed in f(), so seeing arg1
declared as const is of no help at all.
Playing around with this, I discovered that I can declare arg1 as
const in the definition, like this:
void f(const int arg1) { ... }
while not declaring arg1 as const in the declaration/signature/
prototype, like this:
void f(int arg1);
The compiler will compile this just fine, despite that "const" is not
in both lines. (I tried this on Visual C++, g++ on Win32, and g++ on
Linux.)
If you'd like to see a full working program that illustrates what
I'm talking about, here is one:
#include <iostream>
void f(int arg1);
int main(int argc, char ** argv)
{
f(77);
return 0;
}
void f(const int arg1)
{
std::cout << "arg1 = " << arg1 << std::endl;
}
So my question is: Is this an acceptable thing to do (as far as the
C++ language specifications are concerned)?
(Just because it works on all compilers I've tried it on so far
doesn't necessarily mean it's allowed in the standard language, so I'd
like to know for sure if it's a legal thing to do.)
Being able to do this is good in that the maintainers of the
function can see what passed-in arguments are never supposed to
change, while the other maintainers can treat the function as a black-
box and not need to see which passed-in-by-value arguments are const.
Thanks.
-- Jean-Luc