1

Assume I have a class A say has 2 functions funn1 and func2.

When I write func1 first and inside func1 I have a lamda which needs to access A obj members, refer to local var and quite big.

const auto mylamda = [&, this](...) {}

Now I want to use the same lamda in func2. Wondering how to achieve it? Can I define the lamda in class header file?

MingC
  • 63
  • 4
  • 5
    Why lambda rather than a member function ? – Eugene Sep 10 '21 at 02:03
  • If you want to use a lambda inside `func2` when that lambda captures references to local variables in `func1`, then you need to call `func2` from within `func1` and pass it a reference to the lambda – Kevin Sep 10 '21 at 02:07
  • Can you be more specific. Can you show what you've tried, and explain what undesirable or non-working results you are seeing. It's hard to understand what you're asking. Code speaks louder than words. – Sam Varshavchik Sep 10 '21 at 02:07
  • 1
    lambda is unnamed functor; For reuse and avoid code duplication, use a named functor, or a named function which create the lambda. – Jarod42 Sep 10 '21 at 07:51
  • when used the possible lamda in func2, of course it is supposed to access func2's local var (no more func1's local anymore). There are many ways to achieve the purpose as mentioned in above comments. But just to explore whether I could use lamda to achieve. I wrote my lamda in func1 first. later I realized I need its feature inside func2 also. – MingC Sep 11 '21 at 04:08

0 Answers0