230k views
1 vote
Would these statements cause an error? Why or why not? int year = 2019; int yearNext = 2020; int & ref = year; & ref = yearNext;

User Nategood
by
5.0k points

1 Answer

6 votes

Answer: YES

Step-by-step explanation:

int year=2019;

int yearNext=2020;

int &ref=year;

&ref=yearNext; {This line leads to error as references cannot be reassigned}

(//compilation error: lvalue required as left operand of assignment

) lvalue(&ref) is not something which can be assigned.

To get this better, here are the difference between pointers and reference variables

Pointers

Pointers can be incremented

Array can be formed with pointers

Pointers can be reassigned

Pointer can be declared as void

Reference variable

References cannot be incremented

Array cannot be formed with references

References cannot be reassigned as references shares the address of the variable its assigned

References can never be void

User Alex Klock
by
4.4k points