By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

reflection in c++

P: n/a
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.
Sep 22 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Kuberan Naganathan schrieb:
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
Sep 22 '07 #2

P: n/a

Daniel Kay wrote in message...
Kuberan Naganathan schrieb:
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
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

--
Bob R
POVrookie
Sep 22 '07 #3

P: n/a
On 2007-09-22 19:53, Kuberan Naganathan wrote:
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.

--
Erik Wikström
Sep 22 '07 #4

P: n/a

Alf P. Steinbach <al***@start.nowrote in message...
* BobR:
Maybe if you described what a 'reflection' is, we could guide you.

"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?

--
Bob R
POVrookie
Sep 22 '07 #5

P: n/a
BobR wrote:
Alf P. Steinbach <al***@start.nowrote in message...
>* BobR:
>>Maybe if you described what a 'reflection' is, we could guide you.
"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?
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".

--
Thanks
Barry
Sep 23 '07 #6

P: n/a
"Kuberan Naganathan" <ku*****@att.netwrote in message
news:46***********************@roadrunner.com...
: 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/wg...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/wg...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/wg...007/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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Sep 23 '07 #7

P: n/a
On 2007-09-22, Kuberan Naganathan <ku*****@att.netwrote:
>
Does anyone know of a good way to use reflection in c++?
Try this one:
http://www.garret.ru/~knizhnik/cppre...s/reflect.html
Sep 23 '07 #8

P: n/a
On Sep 23, 1:44 am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
"Kuberan Naganathan" <kube...@att.netwrote in message

news:46***********************@roadrunner.com...
: 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/Produ...MSToolkit.html
--
Ira Baxter, CTO
www.semanticdesigns.com
Sep 25 '07 #9

P: n/a
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."

On 25 sep, 23:36, idbax...@semdesigns.com wrote:
On Sep 23, 1:44 am, "Ivan Vecerina"<_INVALID_use_webfo...@ivan.vecerina.comw rote:
"Kuberan Naganathan" <kube...@att.netwrote in message
news:46***********************@roadrunner.com...
: Does anyone know of a good way to usereflectioninc++?
Oct 15 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.