reflection in c++

K

Kuberan Naganathan

Hello all

Does anyone know of a good way to use reflection in c++?

I don't mean simply using rtti or dynamic casting.

I'm talking about java/c# style reflection where an actual instance of
an object can be constructed through reflection and method calls can be
made via the reflection apis.

Thanks.
 
D

Daniel Kay

Kuberan said:
Hello all

Does anyone know of a good way to use reflection in c++?

I don't mean simply using rtti or dynamic casting.

I'm talking about java/c# style reflection where an actual instance of
an object can be constructed through reflection and method calls can be
made via the reflection apis.

As far as I know, C++ does not have any reflection API. But if somebody
does know a way, I would be as interested as the poster of the thread.

Cya
 
B

BobR

Daniel Kay wrote in message...
As far as I know, C++ does not have any reflection API. But if somebody
does know a way, I would be as interested as the poster of the thread.
Cya

C++ does not have a Flagleframp either!
Maybe if you described what a 'reflection' is, we could guide you.

Otherwise, send your code to your printer, then read it in a mirror.
<G>

FAQ http://www.parashift.com/c++-faq-lite
 
A

Alf P. Steinbach

* BobR:
Daniel Kay wrote in message...

C++ does not have a Flagleframp either!
Maybe if you described what a 'reflection' is, we could guide you.

Otherwise, send your code to your printer, then read it in a mirror.
<G>

FAQ http://www.parashift.com/c++-faq-lite

"Reflection" means the ability to obtain information about classes etc.
at run time, and to use that information to create objects and call
functions at run-time. It's very useful for testing and serialization
(and also for doing some things in very inefficient and unsafe ways).
The Java and C# language libraries provide reflection.

Cheers, & hth.,

- Alf
 
G

Guest

Hello all

Does anyone know of a good way to use reflection in c++?

I don't mean simply using rtti or dynamic casting.

I'm talking about java/c# style reflection where an actual instance of
an object can be constructed through reflection and method calls can be
made via the reflection apis.

Standard C++ does not support reflection and as far as I know there are
two ways to get the abilities of reflection in C++. The first is the
intrusive way, where you add stuff to each of your classes, preferably
via some preprocessor, which gives you the capabilities you want. The
second way is to not use C++, but rather some extended version which do
support reflection such as C++/CLR.

Be advised that reflection do have some drawback, I remember reading
some statistics which said that up to 80% of the size of some managed
binaries was for the metadata, I do not know how that translates to
runtime memory. While all of that might not have been necessary for
reflection, a great deal was.

If you could tell us why you want reflection (unless it was for the sake
of it) someone here can probably tell you another, and perhaps better,
way of doing it.
 
B

BobR

Alf P. Steinbach said:
* BobR:

"Reflection" means the ability to obtain information about classes etc.
at run time, and to use that information to create objects and call
functions at run-time. It's very useful for testing and serialization
(and also for doing some things in very inefficient and unsafe ways).
The Java and C# language libraries provide reflection.

Sort of like RTTI coupled with a abstract factory or proxy?
 
B

Barry

BobR said:
Sort of like RTTI coupled with a abstract factory or proxy?
AFAIK,
Some basic reflect, like loading a class by string, querying the members
of a class/object and then invoking them.
To reflect, the language often needs a single-root class like "Object".
 
I

Ivan Vecerina

: Does anyone know of a good way to use reflection in c++?
:
: I don't mean simply using rtti or dynamic casting.
:
: I'm talking about java/c# style reflection where an actual instance of
: an object can be constructed through reflection and method calls can
be
: made via the reflection apis.

As others pointed out, there is no support for reflection
in the C++ standard.

But among the papers that were submitted for consideration
in C++0x, one was quite interestingly addressing this problem:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1471.pdf
Idea: instead of storing lots of run-time information, allow
compile-time code to process type info, and generate the desired
runtime information...
See also, for uses of reflection:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1775.pdf

Unfortunately, this will not be for C++09. See the status of these
papers in:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2389.html
"Not ready for C++0x, but open to resubmit in future"

Food for fantasy...


This said, today, template functions can help easily implement
basic tools like factory functions. Other smart tricks also
help reduce the effort required to implement serialization
(e.g. see the archive interface in boost::serialization,
allowing a single template function to implement both
input and output, avoiding a common duplication).


Regards,
Ivan
 
I

idbaxter

: Does anyone know of a good way to usereflectionin c++?
:
As others pointed out, there is no support forreflection
in the C++ standard.
There's a third way. Process the C++ sources with a C++ source
analysis tool, and extract the "reflection" data you need. A more
sophisticated idea is to apply a C++ program transformation tool
to find the reflection data desired, and act on it to produce the
C++ code to achieve the desired result.

A tool that can do this is the DMS Software Reengineering Toolkit.
It can parse many dialects of C++, do complete name and type
resolution just like the compiler(s), and then transform the source
code according to custom transformation rules. We use DMS
to carry out C++ instrumentation tasks, and massive reengineering
tasks on C++ source code.

See http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html
 
R

remi.chateauneu

This may help, it is basically an implementation of the ideas
suggested here:

http://sourceforge.net/projects/crd

'CRD: A C++ reflection-based data dictionary'

"Lightweight include files adding reflection capabilities to C++.
Generates SQL queries and serialization routines. STL-style iterations
over class members. Compile-time / run-time reflection. Based on
templates and meta-programming. Space and time efficient, very easy to
adapt."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top