|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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... |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
The compiler sees this for #2
void foo(int i) { int local = i; } Using const with a pass by copy argument is useless. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
> 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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
* 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? |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|