C++ lvalues & rvalues / const and etc could anyone help me solidifying these con
ID: 3875560 • Letter: C
Question
C++ lvalues & rvalues / const and etc
could anyone help me solidifying these concepts and explain why?
For each of the following sets of statements, select whether or not the statements will cause a compile error in C++ 11. If they do cause a compile error, choose one of the following reasons (you may use each reason more than once or not at all, and you should write the options you choose in the line below the code): a. No error b. Error: Attempt to assign pointer to a non-pointer type c. Error: Attempt to assign value reference to an rvalue d. Error: Attempt to assign const reference or pointer to non-const data e. Error: Attempt to assign non-const reference or pointer to const data i. int a = 2: const int* b = a: ii. int a = 2: int & b = a: iii. const int a = 2: int b = a: iv. const int a = 2: int & b = 5: v. int a = 2: int const & b = a: vi. const int a = 2: int * const b = &a;Explanation / Answer
i) d. Attempt to assign constant reference or pointer to non constant data.
Here we are assigning pointer variable which is constant to non constant variable. So it is throwing error.
ii) No error
It doesn't have any errors.
iii) No error
It doesn't have any error.
iv) c. Attempt to assign lvalue reference type to an rvalue
In this the left side & b is a reference type and the right side value is an int value. So it doesn't support. For the reference type variable we need to assign some pointer variable or another variable. Reference variable stores the address of the another variable.
v) No error
It doesn't have any error.
vi) e. Attempt to assign non constant reference or pointer to const data.
Because here in this a variable is constant one. We are assigning this as reference variable to another pointer variable which is not constant . In the second statement pointer is defined as int. We need to rewrite the statement by changing pointer symbol after the const i. e. Int const* b=&a. Then it will be compiled successfully.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.