473,396 Members | 1,865 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.

typeid and polymorphic classes

Please consider this code:

class base {};
class derived: public base {};

base *ptr = new derived;
cout << typeid(*base).name << endl;

In this case, I see output of "class base" rather than "class derived".
This is somewhat expected I suppose since my base class does not have any
virtual functions and, therefore, I do not have polymorphic classes.

Adding a virtual destructor to base results in output of "class derived", as
expected.

Can somebody more knowledgeable than I please confirm that this is indeed
the exact expected behavior according to the C++ Standard?

Thank you!
Jul 19 '05 #1
5 8066
Dave Theese wrote:
Please consider this code:

class base {};
class derived: public base {};

base *ptr = new derived;
cout << typeid(*base).name << endl;

In this case, I see output of "class base" rather than "class
derived". This is somewhat expected I suppose since my base class
does not have any virtual functions and, therefore, I do not have
polymorphic classes.

Adding a virtual destructor to base results in output of "class
derived", as expected.

Can somebody more knowledgeable than I please confirm that this is
indeed the exact expected behavior according to the C++ Standard?


It is.

--
Attila aka WW
Jul 19 '05 #2

"Dave Theese" <ch**********@yahoo.com> wrote in message
news:vl************@news.supernews.com...
Please consider this code:

class base {};
class derived: public base {};

base *ptr = new derived;
cout << typeid(*base).name << endl;

In this case, I see output of "class base" rather than "class derived".
This is somewhat expected I suppose since my base class does not have any
virtual functions and, therefore, I do not have polymorphic classes.

Adding a virtual destructor to base results in output of "class derived", as expected.

Can somebody more knowledgeable than I please confirm that this is indeed
the exact expected behavior according to the C++ Standard?


It is. BTW, without a virtual dtor, what do you think delete ptr; is going
to do?
Jul 19 '05 #3

"Duane Hebert" <sp**@flarn.com> wrote in message
news:0B********************@weber.videotron.net...

"Dave Theese" <ch**********@yahoo.com> wrote in message
news:vl************@news.supernews.com...
Please consider this code:

class base {};
class derived: public base {};

base *ptr = new derived;
cout << typeid(*base).name << endl;

In this case, I see output of "class base" rather than "class derived".
This is somewhat expected I suppose since my base class does not have any virtual functions and, therefore, I do not have polymorphic classes.

Adding a virtual destructor to base results in output of "class derived",
as
expected.

Can somebody more knowledgeable than I please confirm that this is
indeed the exact expected behavior according to the C++ Standard?


It is. BTW, without a virtual dtor, what do you think delete ptr; is

going to do?


Undefined, but typically only the base class dtor will be executed.
Jul 19 '05 #4

"Dave Theese" <ch**********@yahoo.com> wrote in message news:vl************@news.supernews.com...

Adding a virtual destructor to base results in output of "class derived", as
expected.


Without a virtual method, the class is NOT polymorphic. 5.2.8 says that
the dynamic type is only checked when it is.
Jul 19 '05 #5

"Dave Theese" <ch**********@yahoo.com> wrote in message
news:vl************@news.supernews.com...
Please consider this code:

class base {};
class derived: public base {};

base *ptr = new derived;
cout << typeid(*base).name << endl;

In this case, I see output of "class base" rather than "class derived".
This is somewhat expected I suppose since my base class does not have any
virtual functions and, therefore, I do not have polymorphic classes.

Adding a virtual destructor to base results in output of "class derived", as expected.

Can somebody more knowledgeable than I please confirm that this is indeed
the exact expected behavior according to the C++ Standard?

Thank you!


Hi ,

Try referring to item 37: Never redefine an inherited non-virtual function
in the "Effective C++: 50 Specific ways to improve your programs and
Designs"
2nd Edition by Scott Meyers.
Some excerpts:
...... nonvirtual functions are statically bound.....on the other hand,
virtual functions are dynamically bound......
Also put print statements in your constructors and destructors of your
classes to see where they are called, which order etc.


Jul 19 '05 #6

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

Similar topics

15
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
5
by: News Admin | last post by:
I have a bunch of classes, instances of which that need to live in shared memory and be accessed by multiple processes. This means that they cannot have a vtable as the addresses in it will be in...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
1
by: Roal Zanazzi | last post by:
Hi all, I'm reading (sloooowly, time permitting) "Modern C++ Design" by Alexandrescu. In chapter 2.4 "Mapping Integral Constants to Types" (but this is not directly related to my question)...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
7
by: karl | last post by:
I've got a abstract class CBase and two classes CPoly1 and CPoly2 that inherit from CBase. Is there a way to find out the classtype (CPoly1 or CPoly2) when iterating over a list of objects for...
2
by: parag | last post by:
I kind of came across this term while studying something related to dynamic_cast, can somebody shed some light on the same
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.