-1

I have a lambda function inside a static class method, like so:

void
MyClass::foo() {
  auto my_lambda = [](int arg) {
    // do stuff
  }
}

Where foo is a static method of MyClass. Now, inside my_lambda I want to reference another static method bar of MyClass. How can I do this? I've seen this question but it seems like this only works for non static methods, since static methods cannot reference this?

shmth
  • 458
  • 3
  • 11

1 Answers1

0

Here's my naive take on it - but I may have misunderstood the question.

#include <iostream>

class MyClass {
private:
    static int variable;                                // just some common data

public:
    static void another(int dest) {                     // receiver
        std::cout << "I'm alive: " << dest << "\n"; 
    }
    static void foo();
};

int MyClass::variable = 4;                              // 

void MyClass::foo() {
    auto my_lambda = [](int arg) {
        another(arg + variable);                        // reference another static method
    };
    my_lambda(123);
}

int main() {
    MyClass apa;

    apa.foo();
}

I'm alive: 127
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108