- #1
rootX
- 478
- 4
dec is a method that gets the parameter by reference, changes its value and returns a reference const int.
Problem [in bold]: In the book it says, "const means that that the object being returned cannot itself by modified later on." But, I changed the return value without any error.
Problem [in bold]: In the book it says, "const means that that the object being returned cannot itself by modified later on." But, I changed the return value without any error.
Code:
#include <iostream>
using namespace std;
const int & dec (int & a)
{
a = 5;
return a;
}
int main()
{
int a = 0;
cout<<"a = "<<a<<endl;
cout<<"Passing to the method ... "<<endl;
[B] int b = dec(a);
b = 6; //***I am chaning the returned type value****[/B]
cout<<"a = "<<a<<" b = "<<b<<endl;
cout <<"Chaning a value ... "<<endl;
a = 6;
cout<<"a = "<<a<<endl;
cout<<"Passing to the method ... "<<endl;
b = dec(a);
cout<<"a = "<<a<<" b = "<<b<<endl;
}