Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
代码:
class A { private: int abc; string xyz; }
在上面的代码中,如何访问声明数据成员的名称 ( abc, xyz) 并将它们打印到控制台?
abc
xyz
我能想到的唯一方法是使用宏:
#define PRINT_VAR(x) std::cout << #x << '\n';
C++ 没有其他语言的反射能力。
如果您的意思是在事先不知道这些名称的情况下以字符串的形式访问成员的名称,那是不可能的——C++ 不支持反射。