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

Is there "reflect" solution in C++?

P: n/a
I just swtiched my working language from java to C++.
In java, there is one mechanism called reflect which enable us to have
knowledge of internals of a given class at run-time. That's one basic
mechanism used by JUnit.
I was told that there is no reflect in C++. I am wondering whether or
not there is so solution to program out such feature.
Any ideas?
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Cheng Mo wrote:
I just swtiched my working language from java to C++.
In java, there is one mechanism called reflect which enable us to have
knowledge of internals of a given class at run-time. That's one basic
mechanism used by JUnit.
I was told that there is no reflect in C++. I am wondering whether or
not there is so solution to program out such feature.
Any ideas?


The short answer is 'no'.

The longer answer is: some development suites support it, but the language
specification does not require reflection. There are many reasons for
this. The primary reason is the different nature of C++ verses Java. With
Java the use of a virtual machine and single inheritance from a universal
base class (the java.lang.Object class).

I certainly understand your desire to have introspection. Roumor has it,
Bjarne Stroustrup is working on some kind of introspection for C++, but
it's hush-hush.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #2

P: n/a
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:<Wp********************@speakeasy.net>...
Cheng Mo wrote:
[...]
I was told that there is no reflect in C++. I am wondering whether or
not there is so solution to program out such feature.
Any ideas?


The short answer is 'no'.
[...]


Steven's reply is clearly correct. Let me just add a couple of
considerations.

Reflection as per the Java definition is actually more of a simple
introspection than full-fledged reflection (as you can find e.g. in
OpenC++ or OpenJava (see
http://www.csg.is.titech.ac.jp/~chiba/openc++.html and
http://www.csg.is.titech.ac.jp/openjava/)). Unfortunately, C++'s idea
of introspection (known as RTTI (Run-Time Type Investigation or
Identification or Information or...)) is even narrower. Forget about
e.g., obtaining a meta-class as you can do in Java, querying it for
method signatures and calling the methods. However, if you're after
really simple stuff such as getting to know the name of a type or
comparing types for equality, you can do that in C++ too (via the
typeid operator).

BUT...

The standard really leaves a lot to be desired in this field, and
dictates hardly anything even about the format of the type name to be
returned. So some compilers go all the way and produce a complete
class name with template parameters and all, while others emit little
more than garbage. This means that if you want to be portable you'd
better be extra careful while using that feature.

Regards,
a.
Jul 22 '05 #3

P: n/a
"Andrea Savigni" <as******@acm.org> wrote in message
news:e5*************************@posting.google.co m...
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:<Wp********************@speakeasy.net>...
Cheng Mo wrote:
[...]
I was told that there is no reflect in C++. I am wondering whether or
not there is so solution to program out such feature.
Any ideas?


The short answer is 'no'.
[...]


Steven's reply is clearly correct. Let me just add a couple of
considerations.

Reflection as per the Java definition is actually more of a simple
introspection than full-fledged reflection (as you can find e.g. in
OpenC++ or OpenJava (see
http://www.csg.is.titech.ac.jp/~chiba/openc++.html and
http://www.csg.is.titech.ac.jp/openjava/)).


Another tool that provides "compile time" reflection for C++ is
the DMS (http://www.semanticdesigns.com/Produ...SToolkit.html).
(DMS also provides source-to-source transformations
and program analyses in much easier-to-specify forms
than OpenC++).

One could, in principle, extract all this "reflection" information
and generate runtime tables that could answer runtime
reflection questions (this is in effect what Java does).
Without dynamic loading, however,
that doesn't seem very useful; any runtime question you
can ask can be answered effectively at compiletime
by such tools, and the results can be used to generate
code before compilation, which goes beyond what Java reflection does.
--
Ira D. Baxter, Ph.D., CTO 512-250-1018
Semantic Designs, Inc. www.semdesigns.com
Jul 22 '05 #4

P: n/a
"Ira Baxter" <id******@semdesigns.com> wrote
Another tool that provides "compile time" reflection for C++ is
the DMS (http://www.semanticdesigns.com/Produ...SToolkit.html).
(DMS also provides source-to-source transformations
and program analyses in much easier-to-specify forms
than OpenC++).

One could, in principle, extract all this "reflection" information
and generate runtime tables that could answer runtime
reflection questions (this is in effect what Java does).
Without dynamic loading, however,
that doesn't seem very useful; any runtime question you
can ask can be answered effectively at compiletime
by such tools, and the results can be used to generate
code before compilation, which goes beyond what Java reflection does.


Point is, there *is* dynamic loading in C/C++ on most platforms, in
the form of dynamic libraries (e.g., dlopen() and friends on unix,
LoadLibrary() and friends on windows). Under this respect, what you
can do in Java you can also do in C++ (even though in Java it is
admittedly easier on the programmer). However, one very interesting
thing you can do in Java is query an object of a previously (i.e., at
compile time) unknown class (loaded via forName()) about its
structure, methods, etc. and call said methods. In ordinary C++ you
can instantiate an object of a previously unknown class (assumed it
was linked as a DL) but you cannot query it nor much less so call its
methods (which of course you don't know about at compile time since
you don't even know about the object's class). I don't know about the
DMS, can you do such things even with dynamically loaded classes? If
so that actually sounds interesting.

Cheers,
a.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.