470,833 Members | 1,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

why arguments are not incompatible

Why the below two code snippet behave differently

Case 1:
#include <iostream>

void foo(const int *i)
{
int *local = i;
}

int main(void)
{
return 0;
}
and Case 2:
#include <iostream>

void foo(const int i)
{
int local = i;
}
int main(void)
{
return 0;
}

compilation command
g++ -Wall test03.cpp -o test03

In case 1 I got the below error.
error: invalid conversion from `const int*' to `int*'

In my understanding for an assignment:
- first the lvalue are rvalue must be of compatible type without
considering the qualifiers.
- then the lvalue must have all or more qualifiers than rvalue.

So its ok that I getting an error for case 1 but why its not happening
in case 2.

please clarify...
Jun 27 '08 #1
7 1328

"sumsin" <su*******@gmail.coma écrit dans le message de news:
cd**********************************...oglegroups.com...
Why the below two code snippet behave differently

Case 1:
#include <iostream>

void foo(const int *i)
{
int *local = i;
}

int main(void)
{
return 0;
}
and Case 2:
#include <iostream>

void foo(const int i)
{
int local = i;
}
int main(void)
{
return 0;
}

compilation command
g++ -Wall test03.cpp -o test03

In case 1 I got the below error.
error: invalid conversion from `const int*' to `int*'

In my understanding for an assignment:
- first the lvalue are rvalue must be of compatible type without
considering the qualifiers.
- then the lvalue must have all or more qualifiers than rvalue.

So its ok that I getting an error for case 1 but why its not happening
in case 2.

please clarify...
in case 2, local is a copy of i, so local has nothing to do with i except
that it is initialized with the same value.
So changing the value of local doesn't affect i.

But if you do

void foo(const int i)
{
int& local = i;
}

it wont work cause now local is a reference to i and therefore changing
local cause i to change. It was not the case with your original example.


Jun 27 '08 #2
On Jun 6, 3:12*pm, sumsin <sumsin...@gmail.comwrote:
void foo(const int *i)
{
* int *local = i;

}
The following is a non-const pointer to a non-const int:

int *p;

The following is a const pointer to a non-const int:

int *const p;

The following are a non-const pointer to a const int:

int const *p;
const int *p;

The following are const pointers to const int's:

int const *const p;
const int *const p;

The problem with your code is that you try to use a "pointer to non-
const" to store the address of a const int. C doesn't allow this,
because it could potentially allow you to alter const data.

Jun 27 '08 #3
The compiler sees this for #2

void foo(int i)
{
int local = i;

}

Using const with a pass by copy argument is useless.
Jun 27 '08 #4
Using const with a pass by copy argument is useless.

Why is it useless?
It seems like there are lots of cases where you may have a variable passed
into a function where the function does not need to change it that a const
value may make the code more readable and less error prone in matainence.
Jun 27 '08 #5
On Jun 6, 9:21*pm, "Matt" <Matt.Ri...@covenanteyes.comwrote:
Using const with a pass by copy argument is useless.

Why is it useless?
It seems like there are lots of cases where you may have a variable passed
into a function where the function does not need to change it that a const
value may make the code more readable and less error prone in matainence.
For some strange reason there seems to be a sizeable amount of people
who are vehemently against using const in a function's parameter list.
I don't know why, I mean I treat function arguments just like normal
automatic variables and so I'll use const where appropriate.
Jun 27 '08 #6
On Jun 7, 8:39 am, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
On Jun 6, 9:21 pm, "Matt" <Matt.Ri...@covenanteyes.comwrote:
Using const with a pass by copy argument is useless.
Why is it useless?
It seems like there are lots of cases where you may have a
variable passed into a function where the function does not
need to change it that a const value may make the code more
readable and less error prone in matainence.
For some strange reason there seems to be a sizeable amount of
people who are vehemently against using const in a function's
parameter list. I don't know why, I mean I treat function
arguments just like normal automatic variables and so I'll use
const where appropriate.
A surprising number of people don't use const on normal
automatic variables either:-).

In the case of parameters, there is a strong argument against
using const in the declarations (what the client code sees),
where it has no effect other than adding verbiage and creating
(a very little) confusion, and probably exposing some interior
details of the function which aren't relevant. And a number of
people insist that the signatures in the definition correspond
letter for letter to those in the declaration.

And of course, the usefulness of const is related to contract,
more than anything, and you don't establish a contract for local
variables. (That doesn't mean that they should never be const.
Only that const-ness typically isn't anywhere near as important
with them.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7
Hi!

Alf P. Steinbach schrieb:
True, the function signatures, as pure declarations,

void foo( int );

and

void foo( int const );

are equivalent.

That does not mean that the 'const' has no effect in a function definition.
Which reads:

void foo1( int i )
{
if(i = 5) //allowed
{
bar();
}
}

void foo2( const int i )
{
if(i = 5) //error
{
bar();
}
}
>Using const with a pass by copy argument is useless.

Sorry, that's incorrect.
Yes, it has its usage.

Frank
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

66 posts views Thread by Darren Dale | last post: by
2 posts views Thread by Steven D'Aprano | last post: by
1 post views Thread by grandeandy | last post: by
9 posts views Thread by Mikhail Teterin | last post: by
5 posts views Thread by Martin Johansen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.