简体   繁体   中英

Capturing the equivalent of 'this' in a lambda in a static function

I have a class looks like this:

class MyClass {
    static Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
    static void Setup(Device* device) {
        auto createVSTask = loadVSTask.then([this, device]() {
            DX::ThrowIfFailed(
                device->CreateVertexShader(&vertexShader));
    }
}

I can't use this in the lambda because the function is static. What should I use instead?

You don't need a this pointer to access static methods or variables. Simply remove this from your capture list and the code will work.

static void Setup(Device* device) {
        auto createVSTask = loadVSTask.then([device]() {
            DX::ThrowIfFailed(
                device->CreateVertexShader(&vertexShader));
    }

You don't need to capture this in order to have access to vertexShader ... it's already accessible. All you need is to capture device :

static void Setup(Device* device) {
    auto createVSTask = loadVSTask.then([device]{
        DX::ThrowIfFailed(device->CreateVertexShader(&vertexShader));
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM