I was checking a book of c++ and the put a function that is designed to make the class functions cascadable. In this book they conventionally make function inside the class to return a reference of the class rather than the class value. I tested returning a class by value or by reference and they both do the same. What is the difference?
#include<iostream>
using namespace std;
/*class with method cascading enabled functions*/
class a{
private:
float x;
public:
a& set(float x){
this->x = x;
return *this;
}
a& get(float& x){
x = this->x;
return *this;
}
a print(){
cout << "x = " << x << endl;
return *this;
}
};
int main(){
a A;
A.set(13.0).print();
return 0;
}
result
PS J:\c-c++> g++ -o qstn question1.cpp
PS J:\c-c++> .\qstn
x = 13
as you will notice this code work as spected. But happens here in detail?