-6

i search for a way to call a method by its string name.

#include <iostream>
#include <string>

class myClass{
    public:
        void method1(int run){
            std::cout << run << std::endl;
        }
        void method2(int run){
            std::cout << run << std::endl;
        }
};

int main(){
    myClass mc;
    std::string call;

    call = "method1";
    mc.call(1);

    call = "method2";
    mc.call(2);
}

But the result, is

‘class Myclass’ has no member named ‘call’

I need response "1" and "2";

EDIT :: Very thank's for your help, i get the next solution (i don't know is good for all cases );

#include <iostream>
#include <string>

class myClass{
public:
    void method1(int run){
        std::cout << "Loaded method => " << run << std::endl;
    }
    void method2(int run){
        std::cout << "Loaded method => " << run << std::endl;
    }
    void _loadMethods(int method, int params){
        switch(method) {
            case 1:
                method1(params);
                break;
            case 2:
                method2(params);
            break;
            default:
                 break;
        }
    }
};

int main(){
    myClass mc;
    std::string method;

    method = "method2";

    if(method == "method1"){
         mc._loadMethods(1, 1);
    }
    if(method == "method2"){
        mc._loadMethods(2, 2);
    }
}

Thank's

O.Palm
  • 162
  • 1
  • 3
  • 15
  • Try using macros – CinCout Aug 28 '17 at 06:10
  • 2
    C != C++. Tag appropriately. – tambre Aug 28 '17 at 06:11
  • 3
    I have to get this out of the way: C and C++ are different languages. Now, to answer your question, c++ doesn't support reflection. Consider creating a map with the function's name as key and the function pointer as value – Vanna Aug 28 '17 at 06:12
  • You cannot do that in C++ without explicitly storing method's name somewhere (maybe with using tricks like macros which may hide that). Information about method names may be lost during compilation and cannot be accessed. – yeputons Aug 28 '17 at 06:12
  • you dont have any **x** att. in the classs... what are you trying to do? – ΦXocę 웃 Пepeúpa ツ Aug 28 '17 at 06:12
  • Do also note the performance impact of the `std::map` method proposed by @Vanna. Unless, of course, it's determinable at compile time - but what's the point anyway then? – tambre Aug 28 '17 at 06:13
  • 5
    Possible duplicate of [How can I add reflection to a C++ application?](https://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application) – tambre Aug 28 '17 at 06:14
  • @tambre - If one needs to call a function by name whilst using a run-time string, I don't think the slight performance hit is the major problem... – StoryTeller - Unslander Monica Aug 28 '17 at 06:15
  • 1
    @Vanna - And the `std::map` lookup, that's not free. A static lookup table may give better performance. Binary search and your'e all set. – StoryTeller - Unslander Monica Aug 28 '17 at 06:16
  • Would you be inclined to share why you want to do this? If this is a "design problem" then a better design is in order. Academic curiosity is another matter. – StoryTeller - Unslander Monica Aug 28 '17 at 06:18

1 Answers1

0

This is not possible in "raw" C++. But... What you are trying to achieve is some kind of Reflection or Class meta-type information.
You can look at this project: http://www.axelmenzel.de/projects/coding/rttr , use Qt: http://doc.qt.io/qt-5/qmetatype.html#details or google for C++ reflection.

Roan
  • 892
  • 15
  • 27