PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > why arguments are not incompatible
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
why arguments are not incompatible

Réponse
 
LinkBack Outils de la discussion
Vieux 06/06/2008, 15h12   #1
sumsin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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...
  Réponse avec citation
Vieux 06/06/2008, 15h29   #2
Eric Pruneau
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why arguments are not incompatible


"sumsin" <sumsin123@gmail.com> a écrit dans le message de news:
cdb01b08-0268-41c4-a160-6e403c67cd69...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.




  Réponse avec citation
Vieux 06/06/2008, 16h14   #3
Tomás Ó hÉilidhe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why arguments are not incompatible

On Jun 6, 3:12pm, sumsin <sumsin...@gmail.com> wrote:

> 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.

  Réponse avec citation
Vieux 06/06/2008, 19h29   #4
metarox
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why arguments are not incompatible

The compiler sees this for #2

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

}

Using const with a pass by copy argument is useless.
  Réponse avec citation
Vieux 06/06/2008, 21h21   #5
Matt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why arguments are not incompatible

> 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.


  Réponse avec citation
Vieux 06/06/2008, 21h27   #6
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why arguments are not incompatible

* metarox:
> [referring to earlier function 'void foo( const int i ) ...']
>
> The compiler sees this for #2
>
> void foo(int i)
> {
> int local = i;
>
> }


Well, sorry, that's incorrect.

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.


> Using const with a pass by copy argument is useless.


Sorry, that's incorrect.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 07/06/2008, 07h39   #7
Tomás Ó hÉilidhe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why arguments are not incompatible

On Jun 6, 9:21pm, "Matt" <Matt.Ri...@covenanteyes.com> wrote:
> > 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.
  Réponse avec citation
Vieux 07/06/2008, 10h07   #8
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why arguments are not incompatible

On Jun 7, 8:39 am, Tomás Ó hÉilidhe <t...@lavabit.com> wrote:
> On Jun 6, 9:21 pm, "Matt" <Matt.Ri...@covenanteyes.com> wrote:


> > > 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:james.kanze@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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17059 seconds with 16 queries