C++从字符串到对象标识符的转换
我正在用 C++ 编写一个程序,该程序从外部文件读取一些数据以设置静态变量的值。
是否可以将字符串转换为对象标识符? (例如,将字符串“CheckBox::Unchecked”转换为对象 CheckBox::unchecked 的标识符)
I'm writing a program in C++ that reads in some data from an external file in order to set the values of static variables.
Is it possible to convert a string to an object identifier? (e.g. convert the string "CheckBox::Unchecked" into an identifier for the object CheckBox::unchecked)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不可以。如果您想这样做,您必须手动解析字符串并自己完成工作。
No. If you want to do this, you will have to parse the string manually and do the work yourself.
不,不是,除非您在程序中定义了映射方法。
不过,您可以创建一个哈希值并进行查找。
No, it is not, unless you have a mapping method defined in your program.
You could create a hash and look this up, however.
这绝对是可能的。你如何做取决于你期望的输入。例如,如果您知道要读取复选框字符串,则为该复选框类创建一个
operator>>()
。如果您不知道要读什么,那么您就处于语法和解析领域。为此,您必须定义语法(何时允许使用“checkbox”字符串?)。一旦你写下了语法,你就可以编写一个词法分析器和一个解析器。有一些工具可以做到这一点。
It's definitely possible. How you do it depends on what input you expect. For example, if you know you're about to read a checkbox string, then create an
operator>>()
for the checkbox class.If you don't know what you're about to read then you're in the grammar and parsing domain. For that, you must define your grammar (when is the "checkbox" string allowed?). Once you have the grammar written down you write a lexer and a parser. There are tools for that.