How to invoke a method?

I am looking to call a method with a dictionary I am coming from c# and just getting into C++. I have looked around and cant seem to fully understand the logic, the code I am looking to replicate is c#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    void Main()
    {
    dicMyDictonary = new Dictionary<string, Delegate>()
        {
            {"myString", new Func<MyClass, int, int>(MyMethod)}
        }

     }

    void Run(string s)
    {   
        var res = dicMyDictonary[s].DynamicInvoke(this, 21);
    }

    void int MyMethod(MyClass, int arg1)
    {
        //blah
    }


How can I invoke a method in C++ the similar way?

Thanks!
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <functional> // 
#include <string>
#include <map> 

struct my_class
{
     int my_method( int ) { std::cout << "my_class::my_method\n" ; /* ... */ return 0 ;  }

     void run( std::string s )
     {
         const auto iter = dicMyDictonary.find(s) ;

         if( iter != dicMyDictonary.end() ) iter->second( this, 21 ) ;
         else std::cerr << "delegate '" << s << "' is not in the dictionary\n" ;
     }
     
     // http://en.cppreference.com/w/cpp/utility/functional/function
     using delegate = std::function< int( my_class*, int ) > ; 

     // http://en.cppreference.com/w/cpp/container/map
     std::map< std::string, delegate > dicMyDictonary  { { "myString", &my_class::my_method } };
};

int main()
{
     my_class mc ;
     mc.run("myString") ; // my_class::my_method
     mc.run("not_there") ; // delegate 'not_there' is not in the dictionary
}

http://coliru.stacked-crooked.com/a/e37ba078520c7b3e
You may also willing to know c++ also has lambda function since c++11
which looks like [capture](parameters){content}

example(std : c++11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <functional> // only for function<...>
using namespace std;

int main(){
   auto lambda_simple = [](){return 1;};
   cout << lambda_simple() << '\n';
   
   int somevar=0;
   auto lambda_can_catch_local_var = [somevar](){return somevar;};
   
   int anothervar=10;
   auto lambda_can_catch_local_var_by_referance = [&anothervar](){++anothervar;};
   lambda_can_catch_local_var_by_referance();
   lambda_can_catch_local_var_by_referance();
   cout << anothervar << '\n';

   auto lambda_can_has_params = [](int i){return i+1;};
   cout << lambda_can_has_params(100) << '\n';
   
   function<int(int)> lambda_can_convert_to_function_implicitly = lambda_can_has_params;
   
   cout << lambda_can_convert_to_function_implicitly(100) << endl;
}


*auto is like var in c#
also note that func in c++ can not do += or -=
Thank you guys so much!

Quick question for you JLBorgs, I am trying to add to the Dictionary but I am getting an error 'error: 'dicMyDictonary' does not name a type':
dicMyDictonary->Add( "Call Second Method", &my_class::SecondMethod

I think I understand what the code is doing but if I can't add a index into the dictionary I don't know if I am fully understanding it.
1
2
3
my_class mc ; // to access the non-static member, an object is required
mc.dicMyDictonary[ "Call Second Method" ] = &my_class::SecondMethod ;
// or: mc.dicMyDictonary.emplace( "Call Second Method", &my_class::SecondMethod ) ; 

http://coliru.stacked-crooked.com/a/2731ce0bc4fe31c3

Read a tutorial on using std::map first: http://www.cprogramming.com/tutorial/stl/stlmap.html
I have been wrestling with
"my_class::dicMyDictonary': list initialization inside member initializer list or non-static data member initializer is not implemented
"


Can you tell me what this is? I am trying to build it for universal 10...
Gah, I am not liking C++...
Last edited on
>Then what are you learning C++ for?
I don't like your attitude but I still have to deal with it. Does that answer your question?
Thanks for your help but I would like to discuss the C++ question, have a good day.
Now, to keep to the question:
I have been wrestling with
"my_class::dicMyDictonary': list initialization inside member initializer list or non-static data member initializer is not implemented
"

Can you tell me what can fix this? I dont understand why I need to initilize it inside of a class and not a struct, I am trying to build it for universal windows 10...
Topic archived. No new replies allowed.