Const int & method (int & parameter)

  • Thread starter Thread starter rootX
  • Start date Start date
  • Tags Tags
    Method Parameter
Click For Summary

Discussion Overview

The discussion centers around the behavior of returning a constant reference from a function in C++, specifically regarding the implications of modifying the returned value and the concept of copying versus referencing in the context of function parameters and return types.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a method that returns a const reference and expresses confusion about modifying the returned value, citing a book's explanation.
  • Another participant clarifies that the variable 'b' is a copy of the return value, not the return value itself, thus allowing changes to 'b' without affecting the const reference.
  • A participant acknowledges the clarification and notes that using 'const int & b = dec(a);' avoids the cost of copying, contrasting it with 'int b = dec(a);'.
  • Discussion includes a historical reference to Fortran's handling of similar issues, highlighting differences in error checking related to constants.
  • One participant inquires about the "cost of copying," leading to a discussion about complex objects and the operations involved in copying them.
  • A participant mentions the need to override an assignment operator for a complex object, indicating a deeper exploration of object management in C++.

Areas of Agreement / Disagreement

Participants generally agree on the distinction between modifying a copy versus a reference, but there is no consensus on the implications of copying complex objects or the specific costs associated with it.

Contextual Notes

Limitations include assumptions about the behavior of const references and the implications of copying in C++, which may depend on the specific types of objects being discussed.

Who May Find This Useful

Readers interested in C++ programming, particularly those dealing with function return types, references, and object management, may find this discussion relevant.

rootX
Messages
480
Reaction score
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
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:
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:
 
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.
 
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 ?
 
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)
{

}
 

Similar threads

Replies
12
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K