<> 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 :

here 24 Line is wrong , The error is displayed as a.getValue() Not a modifiable left value .

For a long time ,
I found it could be changed like this :

So why can we change that ?

Because at this time A pass the civil examinations 9 We have << Operator overload is output A Members of num, here a.getValue() Return to a A Object reference for , Then main23 Line can be output successfully .

Note that No 11 Member function in row A& getValue() Back to A References to , If it is A getValue() Then only A type
, Then main In a.getValue() Will call the copy constructor to create a a Exactly the same object a‘, Then the k Assign to a’ Members of a’.num,a.num unchanged .

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; }

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