473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

check for polymorphism during runtime

Hi all,

is there a simple way to find out during runtime if a class of some type
is polymorphic? I am writing some serialization functions and need to
handle polymorphic and non-polymorphic classes differently. I am sure
there are hundreds of workarounds for this, but I am interested if this
can be done by some clever function with the help of casting and/or rtti
which determines if a class is polymorphic or not.
I found a is_polymorphic template in the boost libraries but didn't get
the trick yet!?

Any ideas?

Greetings from Ulm

Jan
Jun 26 '06 #1
2 1600
Jan Schäfer wrote:
Hi all,

is there a simple way to find out during runtime if a class of some type
is polymorphic? I am writing some serialization functions and need to
handle polymorphic and non-polymorphic classes differently. I am sure
there are hundreds of workarounds for this, but I am interested if this
can be done by some clever function with the help of casting and/or rtti
which determines if a class is polymorphic or not.
I found a is_polymorphic template in the boost libraries but didn't get
the trick yet!?

Any ideas?


You've answered your own question - boost::is_polymorphic (a.k.a
std::tr1::is_polymorphic). It's part of the new extension to the
standard, TR1, and an implementation is also available from here:
http://www.dinkumware.com/manuals/?m...is_polymorphic

There is no portable standard conforming solution AFAIK - boost relies
on a non-portable hack to do with the fact that polymorphic classes have
a vtable pointer, and the standard library version can do it how it likes.

IIRC the boost implementation is based on this non-portable idea:

template <class T>
struct is_polymorphic
{
private:
struct dummy: T
{
virtual void f();
};

public:
static bool const value = sizeof(dummy) == sizeof(T);
};

If adding a virtual function doesn't change the size of the class, it
must already have a vtable pointer. Obviously, a real implementation
will handle non-class types too (returning false).

Tom
Jun 26 '06 #2
Jan Schäfer wrote:
is there a simple way to find out during runtime if a class of some
type is polymorphic?
You mean, if any of its members are declared 'virtual'? Not portably.
I am writing some serialization functions and
need to handle polymorphic and non-polymorphic classes differently. I
am sure there are hundreds of workarounds for this, but I am
interested if this can be done by some clever function with the help
of casting and/or rtti which determines if a class is polymorphic or
not.
I found a is_polymorphic template in the boost libraries but didn't
get the trick yet!?
So, why don't you just use Boost, then?
Any ideas?


Use the work-arounds that you know of.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Samee Zahur | last post by:
Hello, The other day I was rather shocked to find that I couldn't find a good use for runtime polymorphism! Let me explain this a bit further before you get shocked. Any function that I could...
21
by: ravinderthakur | last post by:
hi experts, just out of curosity why polymorphism is allowed to work only with pointers and references to the objects and not with the objects itself. what are the reasons behind...
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
4
by: arunvnk | last post by:
It is not quite clear why the compiler uses rntime polymorphism when the virtual keyword is specified. Since the virtual Keyword is specified,why can't the compiler decide at the runtime...
18
by: voidtwerp | last post by:
what is polymorphism?
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
7
by: doina | last post by:
Hello, Can I have runtime polymorphism using references. I knew that runtime polymorphism could be obtained only by pointers, but now I have tried this in Visual C++ 8.0: #include <iostream>...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.