const parameter
In C++, does the following make sense?
main()
{
int a=10;
fun(a);
}
void fun(const int a)
{
...
}
I can see a program similar to this compile but have linker issues. 开发者_运维百科I just wanted to confirm if assigning a non const var to a const var is apt in C++.
Yes, it is OK.
a
cannot be reassigned in fun()
, exactly as if it would have been declared that way:
void fun(int param)
{
const int a(param);
...
a = 5; // this is illegal and won't compile.
}
As it is passed by copy, there is no impact on main()
's a
anyway. Even if fun()
's a
was declared as non-const and modified.
In a function declaration the top level1 const
is stripped out by the compiler, while it is kept in the definition.
The reason is that the top level element is copied, and from the point of view of the caller it does not really matter whether the copy is constant or not, so on that end const-ness is not an issue. On the other end, in the definition of the function the argument cont-ness is maintained as it can help the compiler detect unintended modifications of the argument inside the function.
So basically:
void foo( int );
void foo( const int ); // redeclaration of the same function
void foo( const int x ) {
++x; // error: x is const!!
}
In the code above there are two exactly equivalent declarations of foo
(the compiler will remove the const
from the declaration), and a single definition. Because the in the signature of foo
in the definition x
is declared as a constant integer, the compiler will complain if you try to increment it.
Some people will use the const
in the definition to get the compiler to flag ++x
as erroneous, but it is not common. On the other hand, whether the argument is declared as int
or const int
, for the caller they are the same.
1 Note that this only applies to the top level const, which is applicable to arguments passed by value and pointers, but never to references (a reference is always const: you cannot reassign the reference). So these are different function declarations:
void f( int& );
void f( int const & );
void f( int* ); // equivalent to void f( int * const )
void f( int const * ); // equivalent to void f( int const * const )
It would make sens if you added return types to your functions and had forward-declared fun
before you called it (or reversed the order of your functions in that compilation unit).
For a plain int
, it doesn't change anything for the caller - the called function can't change the value of that variable in the caller regardless of that qualifier. But for the called function, the parameter is const
, so it can't be modified - that makes a difference, not sure if that generally "usefull" or not.
Now consider this:
int foo(int& a);
int bar(int const& a);
Those are two different beasts. bar
can only read a
, and can take either a plain int
or a const int
(or a const int&
).
foo
, on the other hand, can change a
if it sees fit, and cannot take a const
.
See the Const correctness entry in the C++FAQ Lite for more on this, including information about how this applies (or not) to const-pointers, pointers-to-const and const-pointer-to-const.
It doesn't make much sense because what you get in the function is a copy of the variable, not the variable itself. But this is legitimate.
The "assignment" is ok - a in fun is a new variable which is kept constant inside fun.
But fun should have a return type, i.e. void. And there should be a declaration of fun before using it in main.
You can assign a non-const to a const
精彩评论