Const int & method (int & parameter)

  • Thread starter Thread starter rootX
  • Start date Start date
  • Tags Tags
    Method Parameter
AI Thread Summary
The discussion centers around the behavior of the `dec` function, which returns a reference to a constant integer. A key point of confusion arises from the statement in a book that "const means that the object being returned cannot itself be modified later on." However, participants clarify that the variable `b` in the main function is a copy of the return value, not the return value itself. Changing `b` does not affect the original value referenced by `dec`. The conversation also highlights the difference between using `const int & b = dec(a);` and `int b = dec(a);`, emphasizing that the former avoids the overhead of copying, especially for complex objects. The cost of copying is discussed, particularly in relation to objects that require significant resources to duplicate, such as those with dynamic memory allocations. The thread concludes with a mention of the need to override assignment operators for custom objects, indicating a deeper exploration of object management in C++.
rootX
Messages
478
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)
{

}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top