Whats wrong in ref argument | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Whats wrong in ref argument

Hi We can pass local variable to capture clause of lambda as reference. With this, local variable can be modified from lambda body. It is working fine for me in code below: Question is related to functor. I am trying to achieve same thing with functor class having int& as reference member. Wha's the issue here as I am not getting i modified from functor. https://code.sololearn.com/cmntGs1Rrp5m/?ref=app

2nd Dec 2022, 11:39 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 3
Ketan Lalcheta pass by reference in the class constructor. class myFunctor { int& i; public: This⤵️ myFunctor(int& i) : i(i) {} void operator()() {++i;} }; https://code.sololearn.com/cb9DAcV0IwYT/?ref=app
2nd Dec 2022, 12:49 PM
Bob_Li
Bob_Li - avatar
+ 1
very good YouTube tutorial on functor usage https://youtu.be/qcuYSqHxqLk
2nd Dec 2022, 1:15 PM
Bob_Li
Bob_Li - avatar
+ 1
Thanks for sharing this video... Learnt something new now about use case of functor with template
4th Dec 2022, 8:04 PM
Ketan Lalcheta
Ketan Lalcheta - avatar