1

Without using macros or Boost libraries, is it possible to iterate through a classe's own members in C++?

I know "Reflections" are not natively possible in C++ like they are in Java, C# and Go (heartbreaking), but I don't know if that applies to just classes looking at attributes of other classes or if that also applies to themselves.

I'm hopeful some class minding it's own business might be able to see the attributes of itself somehow at runtime; is this possible?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
  • As stated in one of the answers in that SO Q&A you linked to, no. There is no reflection capability in standard C++. There have been a number of proposals put forward concerned with introducing such a capability but, to date, none have been accepted. – Peter Nov 27 '18 at 04:59

1 Answers1

2

Nonono. C++ is statically typed compiled language; it does not need to know the names of the members at runtime since all access at runtime is done by address; this makes member names useless cruft that does not justify being in the executable. You can't access what's not there.

The only way to know member names at runtime is to include code that explicitly stores the name during the compilation process - i.e. macros.

Amadan
  • 191,408
  • 23
  • 240
  • 301