what exactly does this statement imply: int &a=2? | SoloLearn: Learn to code for FREE!

0

what exactly does this statement imply: int &a=2?

doubt

2/21/2017 1:42:36 PM

Ashish Kumar Verma

10 Answers

New Answer

+4

@Robin's answer is correct - that is the only way that statement can work. The statement int&&a = 2; is in C++11 (and later) and refers to the way 'rvalue references' are declared. If you do just int& a = 2; the compiler will complain, since the literal value 2 is an rvalue, and 'normal' references cannot take rvalues as references. For that to work, you have to assign an lvalue: int x = 2; int& a = x; //x is an lvalue The (main) difference between lvalues and rvalues is you can take the address of an lvalue, but not an rvalue. You in fact have the same problem with pointers, but here it can be extremely dangerous: int* a = 2; //This can compile (if you use a cast), but is most definitely not what you intended and quite dangerous! a is now pointing somewhere in the BIOS of your PC! int* a = &2; //This will not compile, since you cannot take the address of an rvalue. int x = 2; int* a = &x; //This will compile since x is an lvalue and you can take its address.

+1

&& is used in conditional statements. i.e.,if we write: if(firstName == 'John' && lastName == 'Bell') { Response.Write("Welcome John Bell!"); } the Response.Write statement will run only if both variables firstName and lastName match to their condition. Whereas & operator is used for Binary AND operations, i.e., if we write: bool a, b, c; a = true; b = false; c = a & b; Response.Write(c); // 'False' will be written to the web page Here first Binary And operation will be performed on variables a and b, and the resultant value will be stored in variable c.

+1

but we are declaring a variable of int type so here no conditional statement can be assumed i.e int &&a=2 also does not make any sense.

+1

@Ashish, any literal declared in your program is an rvalue, because it is on the 'right- hand' side of the assignment operator, like this: int x = 2; This will be illegal and will not compile : 2 = x; Here, having a literal as an lvalue (on the left-hand side of the assignment operator) clearly does not make sense since we are trying to redefine the meaning of 2. So this is why 2 is an rvalue. Note that only lvalues can be on the left-hand side of the assignment operator, but both lvalues and rvalues can be on the right-hand side, so this is perfectly legal: int x = y; Another difference, as mentioned, is that you can take the address of an lvalue, but not an rvalue.

+1

@Robin, I now see I misread your answer. You are right, but not for the reason you gave. In this statement int&& a = 2; the && is not the logical AND operator. This statement is a declaration (and initialisation) of an rvalue reference. If your answer is true, the following should also compile : int a=3; if(int && a) a = 4; Try it, you will see it will not work.

0

i think its supposed to be written as so. int&&a=2 integer and a is equal to 2

0

many a times i have faced this question in challenges.

0

@Ettienne how is 2 a rvalue?

0

thanks @Ettienne you are real good at explaining things 😊

-1

@Robin but & is an operator right? then can u take it as int