Const int & method (int & parameter)

In summary: This throws an error because you changed the return value without first checking to see if the value has been changed.
  • #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.

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;
}
 
Technology news on Phys.org
  • #2
rootX said:
But, I changed the return value without any error.
No you didn't: you changed b. b is not the return value of your function; it is merely an int value which was initialized to be a copy of the return value of your function.
 
Last edited:
  • #3
Hurkyl said:
No you didn't: you changed b. b is not the return value of your function; it is merely an int value which was initialized to be a copy of the return value of your function.

oook thnx!
So, I was using operator = which makes a deep copy.

This throws error now that I cannot change const value ...
dec(a) = 5;

And that's why book was recommending to use

const int & b = dec(a);
rather than
int b =dec(a);

so that I avoid cost of copying.
:smile:
 
  • #4
old versions of fortran were always fun with stuff like this:

call example(1.0)
b = 1.0
c = b+1.0
end

...

subroutine example(a)
a = 2.0
end

Yep no checking for changing of constants.
 
  • #5
rootX said:
oook thnx!


const int & b = dec(a);
rather than
int b =dec(a);

so that I avoid cost of copying.
:smile:

What is the cost of copying ?
 
  • #6
lonton said:
What is the cost of copying ?

I was talking about complex objects where you perform several operations to make the object copy like deleting all the pointers .. and then providing them new locations:

I need to override something like this function..
const & Object operator:= (const Object &rhs)
{

}
 

FAQ: Const int & method (int & parameter)

What is a const int & method?

A const int & method is a function or procedure that takes in an integer reference as a parameter and returns a constant integer reference. This means that the value of the integer parameter cannot be changed within the method, but the method itself can return a different value as a reference.

What is the purpose of using a const int & method?

The purpose of using a const int & method is to ensure that the value of the integer parameter remains unchanged within the method. This can be useful for ensuring data integrity and avoiding accidental changes to the parameter's value.

How is a const int & method different from a regular int method?

A const int & method differs from a regular int method in that it takes in a constant integer reference as a parameter and returns a constant integer reference. This means that the value of the parameter cannot be changed within the method, and the method itself cannot modify the value of the parameter.

Can a const int & method be used with non-constant integers?

Yes, a const int & method can be used with non-constant integers. However, the method will only be able to access and return a constant reference to the integer passed in as a parameter, even if the original integer is not declared as constant.

What are the advantages of using a const int & method?

Some advantages of using a const int & method include ensuring data integrity, preventing accidental changes to the parameter's value, and making it easier to read and understand the code as the method clearly states that it will not modify the parameter's value.

Back
Top