将lambda传递给对象并从lambda内部修改该对象

时间:2016-09-03 09:09:42

标签: c++ c++11 lambda capture

我的代码如下:

Sub Button_Click()

    'Declare the variables
    Dim DataSheet As Worksheet
    Dim SearchSheet As Worksheet
    Dim SearchRange As Range
    Dim rCell As Range
    Dim i As Long

    'Set the variables
    Set DataSheet = ThisWorkbook.Sheets("DATA")
    Set SearchSheet = ThisWorkbook.Sheets("SEARCH SHEET")

    'Clear content of search sheet
    SearchSheet.Cells.Range(SearchSheet.Cells(2, 1), SearchSheet.Cells(Rows.Count, 3)).ClearContents

    'I went from the cell row2/column1 and go down until the last non empty cell
    Set SearchRange = DataSheet.Range(DataSheet.Cells(2, 1), DataSheet.Cells(Rows.Count, 1).End(xlUp))

    i = 1

    For Each rCell In SearchRange 'loop through each cell in the range

        'Check if the cell is equal to value of TextBox1
        If rCell.Text = ActiveSheet.OLEObjects("TextBox1").Object.Text Then
            i = i + 1   'Row number +1 everytime when occurance is found
            SearchSheet.Cells(i, 1) = rCell.Offset(0, 0) 'ITEM CODE
            SearchSheet.Cells(i, 2) = rCell.Offset(0, 1) 'ITEM NAME
            SearchSheet.Cells(i, 3) = rCell.Offset(0, 2) 'QUANTITY
        End If

    Next rCell
End Sub

有没有什么方法可以捕获LambdaContainer'cont',它指的是从哪里调用lambda? (顺便说一下,我不使用指针)

谢谢你们这么棒的答案:)我想我现在可以开始工作了

3 个答案:

答案 0 :(得分:3)

不是捕获cont,而是将其传递给存储的lambda:

class LambdaContainer
{ 
private:
    std::function<void(LambdaContainer&)> f;

public:
    float x = 10;

    void call_f()
    {
        f(*this);
    }
};

void someFunction()
{
    MyClass ins;
    LambdaContainer cont;
    cont.f = [](LambdaContainer& self)
    {    
        self.x = 10;
    };

    ins.c = cont;
    aVectorSomewhere.push_back(ins);
}

如果要调用lambda,只需从现有实例执行LambdaContainer::call_f,或者根据您的设计添加一种从call_f调用MyClass的方法。

答案 1 :(得分:3)

不要不必要地复制或分配东西,这样就可以了(更不用说更有效了):

struct LambdaContainer { 
    std::function<void(void)> f;
    float x = 10;
};   
struct MyClass {
    LambdaContainer c;
    MyClass(const MyClass&) = delete; // noncopyable
    MyClass& operator=(const MyClass&) = delete;
};
void someFunction(){
    MyClass ins;
    ins.c.f = [&ins](){
        ins.c.x = 10;
    };
    aVectorSomewhere.emplace_back(std::move(ins));
}

答案 2 :(得分:2)

MyClass*更改为动态分配,并将std::unique_ptr指针存储在向量中(使用struct LambdaContainer { std::function<void(void)> f; float x = 10; }; struct MyClass { LambdaContainer c; }; std::vector<std::unique_ptr<MyClass>> aVectorSomewhere; void someFunction() { std::unique_ptr<MyClass> ins(new MyClass); LambdaContainer &cont = ins->c; cont.f = [cont]() { cont.x = 10; }; aVectorSomewhere.push_back(std::move(ins)); } )。这样,lambda捕获的地址在对象的生命周期内不会改变,因为不涉及复制。

aVectorSomewhere[index]->c.f();

然后,稍后调用lambda:

{{1}}