C++,非托管,类,成员:如何(如果有的话)列出类中的所有成员?
可能的重复:
迭代结构变量。
所以我有一个头文件和一个代码文件。该类是将从存储过程中查询的视图的表示。对于每个列。鉴于类中有一个数据成员。 目前在代码中我们有这样的东西:
Load( Reader reader)
{
m_col1 = reader("m_col1");
m_col2 = reader("m_col2");
..
}
我怎样才能编写一个代码来迭代成员变量并给我这样的代码:
Load( Reader reader)
{
For (each str in ArrayOfMemberVariables)
variableLValue(str) = reader(str); // str = m_col1, m_col2 ...
}
Possible Duplicate:
Iterate through struct variables.
So I have a header file and a code file. The class is representation of a View that will be queried from stored proc. For each col. in view there is one data member in class.
Currently in code we have something like this:
Load( Reader reader)
{
m_col1 = reader("m_col1");
m_col2 = reader("m_col2");
..
}
How can I write a code that will iterate through member variables and give me code like:
Load( Reader reader)
{
For (each str in ArrayOfMemberVariables)
variableLValue(str) = reader(str); // str = m_col1, m_col2 ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C++反射问题已经被提出过好几次了。不幸的是,除非您自己管理元数据,否则这是不可能的。有关更多详细信息,请参阅此问题。
The C++ reflection question has been brought up several times. Unfortunately, it's not possible unless you manage the metadata yourself. See this question for more details.
如果您的意思是像 PHP 中那样动态声明变量名称(使用其他变量名称),则不能在 C++ 中执行此操作。
在 C++ 中,您没有像 Java 那样的反射概念,在 Java 中,您可以内省类的变量和围绕该变量的代码,以执行诸如序列化之类的操作,并提前了解类成员。
If you mean declaring variables names dynamicly like in PHP for example (using other variable names), you can't do that in C++.
In C++ you don't have the notion of reflection like in Java where you can introspect the variables of your class and code around that to do things like serialization with knowing in advance the class members.