6

is this possible to get all the variables and methods of a class at runtime? if yes then how? I did this in C# using Reflection. but now i am working in C++.

Asish Biswas
  • 221
  • 1
  • 4
  • 17
  • I have a template for custom classes. within that template i need to access variables of the class object. i will build a query string dynamically for db operations. i have designed the classes variables according to my database table. so, if i can get all the variables name and data, i will be able to generate my required query string. – Asish Biswas Oct 25 '10 at 19:05
  • You can have a look at the Qt library, it provides some reflection capabilities trough the use of a preprocessor (moc). – Luc Touraille Oct 26 '10 at 12:14

5 Answers5

6

There is no way to do what you are asking in C++. As suggested in the other answer, RTTI can help you, but is probably not what you need.

If you describe in more details what you are trying to do and why you need reflection, we can probably suggest other solutions in C++.

Benoit Thiery
  • 6,325
  • 4
  • 22
  • 28
  • I have a template for custom classes. within that template i need to access variables of the class object. i will build a query string dynamically for db operations. i have designed the classes variables according to my database table. so, if i can get all the variables name and data, i will be able to generate my required query string. – Asish Biswas Oct 25 '10 at 19:04
  • 1
    Then you need your template to include building of the query or some method to return you the list of fields of your table. Reflection APIs are usually not very efficient and it is better to add methods to your template to get this information in the way that best suits your needs. – Benoit Thiery Oct 25 '10 at 19:19
2

You can use RTTI in C++.

This is just an opinion: It's not as easy/straighforward as C#'s reflection API.

Also check out this SO question.

Community
  • 1
  • 1
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
1

While you can determine the type of an object using RTTI, C++ is not fully reflective and you cannot take a normal C++ class and determine what methods or variables it has.

Sydius
  • 13,567
  • 17
  • 59
  • 76
0

I don't think there is a way to enumerate members of a class. A while ago I needed the very same thing, and in the end settled with manual registration of every member and every class of interest in my own container. Even then, members were all of the same basic type (replacement for Object class from C#). Enumerating members and calling the base function of each member is easy then. It works, and I'm happy.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • I've seen something similar automated with a custom pre-processing step for C to provide reflection of structs. – Sydius Oct 25 '10 at 19:00
0

I find this repo. https://github.com/Celtoys/clReflect Create reflection data and load.

Jea Yoon Lee
  • 1
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 17 '22 at 01:16