Unclear about link description in C ++ Primer

The book says: since links are not objects, we cannot define a link to a link.

int ival = 1024;
int &refVal = ival;
refVal = 2;
int ii = refVal;
int &refVal3 = refVal; // isn't this a definition of ref to ref?
int i = refVal;
int &refVal4 = 10;
double dval = 3.14;
int &refVal5 = dval;

However, this line is not an error, because refVal3 says that it is just another alias for refVal, and refVal is just another name for ival (refVal3 is attached to the object that refVal is attached to, which is ival) ... therefore refRal and refVal3 refers to the ival initializer.

It makes sense, but if this is not a definition of a link to a link, then what exactly does the book mean when it mentions "Since links are not objects, we cannot define a link to a link." ??

Can someone please give me an example?

+4
source share
2 answers

Your understanding is correct.

int &refVal3 = refVal;

refVal3 refVal . ival.

, " , ."?

. . . , , , , . . . , , , , .

- ?

. . . .

+5

(, T & &) ++.

T - ( int, ):

(, cv-qual like const T& , const T & & .)

. :

int main()
{
    int ival = 1024;
    int &refVal = ival;
    int & &refRefVal = refVal;   // wrong
}

, , int & &. , , .

( , , . , int & & , int & - . Collapsing - , , .)

Clang 3.8, :

error: 'refRefVal'

. , Microsoft Visual ++ :

C2529: 'refRefVal':

, , .

, . , , , . , ; .

, (int &refVal3 = refVal;), , , . , , .

int, 1024:

int ival = 1024;

lvalue, int:

int &refVal = ival;

2 int, refVal , :

refVal = 2;

int, , , refVal , :

int ii = refVal;

lvalue , , refVal , :

int &refVal3 = refVal;

, , , .

, int &refVal3 = refVal; - , - , refVal, int, .

, T & &, ?

using Ref = int&;
using RefRef = Ref&; // I named this poorly, it not really a reference to a reference!

, RefRef int& &. . , , :

using RefRef = int&;

, , , templates, , , , ( , , , - , .)

-, . ++ .

+1

Source: https://habr.com/ru/post/1682176/


All Articles