简体   繁体   English

对名称在字符串中的函数进行函数调用 - C++

[英]Making a function call to a function whose name is inside a string - C++

I am extracting a string from a .txt file and saving it in a variable:我正在从.txt文件中提取一个字符串并将其保存在一个变量中:

std::string line = "The king's name is getKingName()";

Lets assume that getKingName() is a function that returns a King class' name data member.让我们假设getKingName()是一个返回King类的name数据成员的函数。

How can I make a call to getKingName() when the string variable looks like that?当字符串变量看起来像这样时,如何调用getKingName()

As far as I know, C++ does not provide such kind of functionality to interpolate functions call inside a string.据我所知,C++ 没有提供这种在字符串中插入函数调用的功能。 All you can do implement your own logic to do that.你所能做的就是实现你自己的逻辑来做到这一点。 Like,喜欢,

1) define all the valid methods like this, 1)像这样定义所有有效的方法,

string getKingName(){
 return "Some name";
}

string otherMethods(){
  return "other values";
}

2) One helper method for mapping of function call 2) 一种函数调用映射的辅助方法

string whomToCall(string methodName){
    switch(methodName){
       case "getKingName()": 
          return getKingName();
          break;
       case "otherMethods()": 
          return otherMethods();
          break;
       default: 
          return "No such method exist";       
    }
}

3) break the line in tokens(words), read one by one and check for following condition also if token starts with any alphabetical character and ends with "()" substring 3)在tokens(words)中换行,一一读取并检查以下条件是否token starts with any alphabetical character and ends with "()" substring

    istringstream ss(line);
    do { 
        string token; 
        ss >> token; 

        if(isMethod(token))
           cout << whomToCall(token) << " ";
         else
           cout << token<< " "; 
    } while (ss); 

4) isMethod() to check if token's value can be a valid method name 4) isMethod() 检查令牌的值是否可以是有效的方法名称

bool isMethod(string token){
   int n= token.length();
   return isalpha(token[0]) && token[n-2]=='(' && token[n-1] == ')' ;
}

This would be the easiest solution, but I think your problem consists of several such calls?这将是最简单的解决方案,但我认为您的问题包括几个这样的调用?

std::string line = "The king's name is getKingName()";
if (line.find("getKingName()") != std::string::npos) {  
    King name = getKingName();
}

Amended修订

This answer is a little off subject.这个答案有点题外话。 I will leave it up, because others might find it relevant, but I agree with other answers, a simple map->function will work better for your case.我会留下它,因为其他人可能会发现它相关,但我同意其他答案,一个简单的 map->function 更适合您的情况。

This is not supported by C++.这不受 C++ 支持。 C++ is not an interpreted language. C++ 不是解释型语言。 If you you want to do things like this, why not use an interpreted language, which do these sorts of things by default.如果你想做这样的事情,为什么不使用解释性语言,默认情况下会做这些事情。 Languages like lua are designed to call C/C++ functions with an interpreted language, with a small overhead. lua 之类的语言旨在使用解释性语言调用 C/C++ 函数,开销很小。

However, if you really need to do this, it is possible, depending on your operating system.但是,如果您确实需要这样做,则有可能,具体取决于您的操作系统。 For example,例如,

  1. On windows start with dbghelp .在 Windows 上以dbghelp 开头 You will need to build a pdb , (eg build with symbols).您将需要构建一个pdb ,(例如使用符号构建)。
  2. On linux, you will also need to build symbols (-g), and use something like dlsym see here for a discussion.在 linux 上,您还需要构建符号 (-g),并使用dlsym 之类的东西,请参见此处的讨论。

That said, there are lots of gotchas doing it this way.也就是说,有很多问题是这样做的。 Optimization can get in the way (best to disable them).优化可能会妨碍(最好禁用它们)。 Also best to avoid dynamic linking (prefer static).也最好避免动态链接(最好是静态的)。 You will also need to cope with C++ name mangling (the name of the function is not the name of your function in C++).您还需要处理 C++ 名称修改(函数名称不是 C++ 中的函数名称)。 see https://blog.oakbits.com/how-to-mangle-and-demangle-ac-method-name.html .https://blog.oakbits.com/how-to-mangle-and-demangle-ac-method-name.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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