<> Do it today c++ Problems encountered

<> How to use the same member function in a class You can output a value It can also be used as a modifiable left value :

Above A::getValue() To return a int,
In the figure below getvalue() Obviously not modifiable lvalue :

Because we need to realize the above 23,24 That's ok ,
For a long time ,
I found it could be changed like this :
Add overload function << And put the getValue() The return type of is changed to A&:

Combined, a member function in a class can be used as a modifiable left value and directly << output

The code is as follows , I don't know how to try it on my own compiler , understand :
class A { public: int num; A(int t) { num = t; } A(const A& p) { num = p.num; }
friend ostream& operator <<(ostream& out,const A p) { out << p.num; return out;
} int getValue() { return this->num; } A& getValue(){ return this->num; } }; int
main() { int m=3, k=10; A a(m); cout << a.getValue() << endl; a.getValue() = k;
cout<<a<<endl; return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Some East 14 Pay change 16 salary , Sincerity or routine ? Browser kernel ( understand )java Four functional interfaces ( a key , simple )HashMap Explain in detail html Writing about cherry trees , Writing about cherry trees os Simple use of module