l***1 发帖数: 22 | 1 比如有一个class
class student
{
int studentID;
float height;
};
studentA student;
然后在运行中,要找出有多少member在studentA里,并且知道它们的名字,类型,得到
它们的指针。
还是老老实实在程序里写出各变量名? |
t****t 发帖数: 6806 | 2 不管是runtime还是compile time,C++是不能枚举member的.
【在 l***1 的大作中提到】 : 比如有一个class : class student : { : int studentID; : float height; : }; : studentA student; : 然后在运行中,要找出有多少member在studentA里,并且知道它们的名字,类型,得到 : 它们的指针。 : 还是老老实实在程序里写出各变量名?
|
b***i 发帖数: 3043 | 3 你的目的是什么?
【在 l***1 的大作中提到】 : 比如有一个class : class student : { : int studentID; : float height; : }; : studentA student; : 然后在运行中,要找出有多少member在studentA里,并且知道它们的名字,类型,得到 : 它们的指针。 : 还是老老实实在程序里写出各变量名?
|
x****u 发帖数: 44466 | 4 虽然C++本身做不到,不过不少framework可以实现类似功能。
【在 l***1 的大作中提到】 : 比如有一个class : class student : { : int studentID; : float height; : }; : studentA student; : 然后在运行中,要找出有多少member在studentA里,并且知道它们的名字,类型,得到 : 它们的指针。 : 还是老老实实在程序里写出各变量名?
|
l***1 发帖数: 22 | 5 能否举几个例子?
多谢。
【在 x****u 的大作中提到】 : 虽然C++本身做不到,不过不少framework可以实现类似功能。
|
l***1 发帖数: 22 | 6 比如想自动存一个Class里的所有变量。想编写一个universal的函数。
不过看到 Boost和OpenCV的XML saving都做不到这点。
【在 b***i 的大作中提到】 : 你的目的是什么?
|
l***1 发帖数: 22 | 7 Thank you.
【在 t****t 的大作中提到】 : 不管是runtime还是compile time,C++是不能枚举member的.
|
b***i 发帖数: 3043 | 8 如果可以,能写出你希望的语法吗?就是这个函数的声明和使用的例子?
【在 l***1 的大作中提到】 : 比如想自动存一个Class里的所有变量。想编写一个universal的函数。 : 不过看到 Boost和OpenCV的XML saving都做不到这点。
|
x****u 发帖数: 44466 | 9 比如说COM里面的automation,就部分实现了这个功能。
【在 l***1 的大作中提到】 : 能否举几个例子? : 多谢。
|
tc 发帖数: 21 | 10 为存数据的话可用protocol buffer
http://code.google.com/p/protobuf/
【在 l***1 的大作中提到】 : 比如有一个class : class student : { : int studentID; : float height; : }; : studentA student; : 然后在运行中,要找出有多少member在studentA里,并且知道它们的名字,类型,得到 : 它们的指针。 : 还是老老实实在程序里写出各变量名?
|
|
|
d***q 发帖数: 1119 | 11
BSON as well...but less declarative.
【在 tc 的大作中提到】 : 为存数据的话可用protocol buffer : http://code.google.com/p/protobuf/
|
i*********n 发帖数: 58 | 12 What do want to do exactly? |
l*******G 发帖数: 1191 | 13 in matlab this is called fieldnames() function, not sure how they realized
it. |
J*****n 发帖数: 4859 | 14
名字和变量还是很容易拿到的,至于指针就太底层了。
【在 l***1 的大作中提到】 : 比如有一个class : class student : { : int studentID; : float height; : }; : studentA student; : 然后在运行中,要找出有多少member在studentA里,并且知道它们的名字,类型,得到 : 它们的指针。 : 还是老老实实在程序里写出各变量名?
|
D*******a 发帖数: 3688 | 15 C++ does not have reflection, but you may be able to use some sophisticated
macro to achieve this.
【在 l***1 的大作中提到】 : 比如有一个class : class student : { : int studentID; : float height; : }; : studentA student; : 然后在运行中,要找出有多少member在studentA里,并且知道它们的名字,类型,得到 : 它们的指针。 : 还是老老实实在程序里写出各变量名?
|
t****t 发帖数: 6806 | 16 请教怎么自动拿到"名字和变量"?
【在 J*****n 的大作中提到】 : : 名字和变量还是很容易拿到的,至于指针就太底层了。
|
U9 发帖数: 25 | 17 Visual C++, I think, they maybe can do that.
【在 t****t 的大作中提到】 : 请教怎么自动拿到"名字和变量"?
|
f*****e 发帖数: 2992 | 18 记得是不是有个
__function__的宏,集采函数名字。
sophisticated
【在 D*******a 的大作中提到】 : C++ does not have reflection, but you may be able to use some sophisticated : macro to achieve this.
|
a9 发帖数: 21638 | 19 那也是编译过程用的。
站: BBS 未名空间站 (Fri Jun 15 18:27:41 2012, 美东)
【在 f*****e 的大作中提到】 : 记得是不是有个 : __function__的宏,集采函数名字。 : : sophisticated
|
l*******G 发帖数: 1191 | 20 Debug 时显然可以,那么把GCC 修改一下,在每个class和structure里加一个return_
members()函数,貌似可以。 |
k********u 发帖数: 148 | 21 C++ Prime 里曾经曰过 (记得是)用public函数return可以访问私有成员。(同意楼
上的) |