a problem in reading 《C++ Primer》

Z

zhou xiang

1.when I write the following codes:

....
int ival = 1024;
int *p = &ival;
int *&rval = p;
....

there is no errors.

2.When I write the following codes:
....
int ival = 1024;
//int *p = &ival;
int *&rval = &ival;
....
the complier shows errors :"cannot convert from 'int *' to 'int *& '
A reference that is not to 'const' cannot be bound to a non-lvalue"

3.When I write the following codes:

....
const int icval = 1024;
const int *const &ricval = &icval;
....

There is no errors.

What is the difference in these codes?
 
J

JKop

zhou xiang posted:
1.when I write the following codes:

....
int ival = 1024;
int *p = &ival;
int *&rval = p;
....

there is no errors.

2.When I write the following codes:
....
int ival = 1024;
//int *p = &ival;
int *&rval = &ival;
....
the complier shows errors :"cannot convert from 'int *' to 'int *& '
A reference that is not to 'const' cannot be bound to a non-lvalue"


Similarly:

p = 0; //legal

&ival = 0; //illegal


3.When I write the following codes:

....
const int icval = 1024;
const int *const &ricval = &icval;
....

There is no errors.

What is the difference in these codes?


The following are all temporaries. A temporary is an r-value, but it is also
non-const. Because they are an r-value, you cannot bind them to a non-const
reference. Why? Because this gives you l-value access to them, as in:

int main()
{
int& blah = int(); //If this were legal,

blah = 5; //then we'd have l-value access
}


To prevent this, we have:

int main()
{
int const &blah = int();

blah = 5; //Illegal, (but because it's const)
}


Another reason why a temporary can only be bound to a const reference:

void Func(int &k)
{
k += 4;
}


int main()
{
float t = 6.8;

Func(t);
}


If the above were legal, then "Func" would be passed a temporary ( an "int"
temporary would be composed from the "float" ), which would then be altered
by "Func". The problem: "t" in "main" would remain unchanged.


And the end of all this, you have the question:

If a temporary is non-const, then why is it an r-value? The only reason I
can see is the "t" problem as shown above.


-JKop
 
V

Victor Bazarov

zhou said:
1.when I write the following codes:

....
int ival = 1024;
int *p = &ival;
int *&rval = p;

A reference is made to refer to a real object 'p', a pointer to int.
....

there is no errors.

2.When I write the following codes:
....
int ival = 1024;
//int *p = &ival;
int *&rval = &ival;

An attempt was made to create a non-const reference to an r-value,
a temporary. It's not allowed.
....
the complier shows errors :"cannot convert from 'int *' to 'int *& '
A reference that is not to 'const' cannot be bound to a non-lvalue"

3.When I write the following codes:

....
const int icval = 1024;
const int *const &ricval = &icval;

A const reference is bound to a temporary (r-value). That's OK.
....

There is no errors.

What is the difference in these codes?

See above.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top