C++ Primer example 7.2.2



 DEVELOP > c-Plus-Plus > C++ Primer example 7.2.2

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "arnuld"
Date: 12 Aug 2007 04:00:42 AM
Object: C++ Primer example 7.2.2
this is from C++ Primer 4/e example 7.2.2 on page 235 titled:
"References to const are more flexible"
#include <iostream>
/* increments the argument by 1 */
int incr( int& i)
{
++i;
}
int main()
{
short v1 = 0;
const int v2 = 42;
int v3 = incr(v1); /* error: must be error */
v3 = incr(v2); /* error: v2 is const */
v3 = incr(0); /* error: argument is rvalue */
v3 = incr( v1 + v2 );/* error: addition yields rvalue */
int v4 = incr(v3); /* OK: v3 is anon-const object of type int */
return 0;
}
/* OUTPUT
[arnuld@arch cpp] $ g++ -ansi -pedantic -Wall -Wextra 7.2.2.cpp
7.2.2.cpp: In function 'int main()':
7.2.2.cpp:15: error: invalid initialization of reference of type
'int&' from expression of type 'short int'
7.2.2.cpp:4: error: in passing argument 1 of 'int incr(int&)'
7.2.2.cpp:16: error: invalid initialization of reference of type
'int&' from expression of type 'const int'
7.2.2.cpp:4: error: in passing argument 1 of 'int incr(int&)'
7.2.2.cpp:17: error: invalid initialization of non-const reference of
type 'int&' from a temporary of type 'int'
7.2.2.cpp:4: error: in passing argument 1 of 'int incr(int&)'
7.2.2.cpp:18: error: invalid initialization of non-const reference of
type 'int&' from a temporary of type 'int'
7.2.2.cpp:4: error: in passing argument 1 of 'int incr(int&)'
7.2.2.cpp:20: warning: unused variable 'v4'
[arnuld@arch cpp] $
*/
well i expected those errors but why the authors (Lippman, Lajoie &
Moo) think that initialising "v4" with the value of "v3" is ok (see
the author's comment) when "v3" itself is a compile time error
?
.

User: "Frank Birbacher"

Title: Re: C++ Primer example 7.2.2 12 Aug 2007 04:27:10 AM
Hi!
arnuld schrieb:

/* increments the argument by 1 */
int incr( int& i)
{
++i;
}

And returns what?

int v4 = incr(v3); /* OK: v3 is anon-const object of type int */

[snip]

well i expected those errors but why the authors (Lippman, Lajoie &
Moo) think that initialising "v4" with the value of "v3" is ok (see
the author's comment) when "v3" itself is a compile time error

Well, I guess they are trying to say that "if v3 was initialised you
could use it as a parameter to incr and save the result of that call in v4".
Frank
.

User: "Gianni Mariani"

Title: Re: C++ Primer example 7.2.2 12 Aug 2007 04:14:43 AM
arnuld wrote:

this is from C++ Primer 4/e example 7.2.2 on page 235 titled:
"References to const are more flexible"


#include <iostream>

/* increments the argument by 1 */
int incr( int& i)
{
++i;

.... did we miss a return expression here ?

}


int main()
{
short v1 = 0;

....

int v4 = incr(v3); /* OK: v3 is anon-const object of type int */


well i expected those errors but why the authors (Lippman, Lajoie &
Moo) think that initialising "v4" with the value of "v3" is ok (see
the author's comment) when "v3" itself is a compile time error

I don't understand why v3 is an anon const object.
.
User: "Frank Birbacher"

Title: Re: C++ Primer example 7.2.2 12 Aug 2007 04:39:54 AM
Hi!
Gianni Mariani schrieb:

arnuld wrote:

int v4 = incr(v3); /* OK: v3 is anon-const object of type int */

[snip]

I don't understand why v3 is an anon const object.

Because it is not an anonymous const object, but "a non-const" object.
There is only a space missing in the code comment.
Frank
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER