tags:

views:

85

answers:

4

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++.

+1  A: 

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.

Pablo Santa Cruz
+5  A: 

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
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.
A.Biswas
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
+1  A: 

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
A: 

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
I've seen something similar automated with a custom pre-processing step for C to provide reflection of structs.
Sydius