typeid and polymorphic classes

D

Dave Theese

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!
 
A

Attila Feher

Dave said:
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.
 
D

Duane Hebert

Dave Theese said:
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?
 
R

Ron Natalie

Dave Theese said:
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.
 
D

Dave Theese

Duane Hebert said:
derived",

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.
 
G

Govindan

Dave Theese said:
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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top