mercredi 24 mai 2017

Lambda function to call member function that has access to class members

Asked this before but didn't get an answer. Cleaned up the example code and removed any Qt. Now what I want to achieve is a function that gets called with a command string and branches to the appropriate function, instead of having something like

if ( str == "Task1" ) task1();  // QString if anyone wonders about the == :)
else if( str == "Task2" ) ) task();
// and so on

I want to put the call functions in a has and call them with a simple hash lookup. It works as it is calling the function but it seems it doesn't have member access. The below code prints out

task1: 0

task2: 0

So is that the expected behavior?

If yes, how can I get it to work?

If no, is it a bug in VS2017?

#include "stdafx.h"

#include <map>
#include <string>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

struct MyClass
{
    MyClass() {
        m_functions.insert( pair<string, function<void()> >( string( "Task1" ), function<void()>( [this]() { task1(); } ) ) );
        m_functions.insert( pair<string, function<void()> >( string( "Task2" ), function<void()>( [this]() { task2(); } ) ) );
}

std::map<std::string, std::function<void()> >m_functions;
int m_memberValue = 0;

void callFunc( string key )
{
    m_memberValue = 42;
    auto func = m_functions.find( key );
    if ( func != m_functions.end() )
    {
        func->second();
    }
}

void task1()
{
    cout << "task1: " << m_memberValue << endl;
}

void task2()
{
    cout << "task2: "  << m_memberValue << endl;
}

};


int main()
{
    MyClass object1;
    MyClass object2;
    vector<MyClass>objects;
    objects.push_back( object1 );
    objects.push_back( object2 );

    objects[0].callFunc( string( "Task1" ) );
    objects[1].callFunc( string( "Task2" ) );
    string str;
    getline( cin, str );
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire