0

Code:

class A
{
    private:
        int abc;
        string xyz;
}

In the above code, how can I access the names by which data members are declared (abc, xyz) and print them to console?

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Udit Bhardwaj
  • 1,761
  • 1
  • 19
  • 29

2 Answers2

2

The only way I can think of is using a macro:

#define PRINT_VAR(x) std::cout << #x << '\n';

C++ doesn't have the reflection capabilities of other languages.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
0

If you mean accessing the names of members, as strings, without knowing those names beforehand, that's not possible - C++ doesn't support reflection.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82