C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Help with dynamic_cast

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Maciej Sobczak
Guest





PostPosted: Sat Aug 23, 2003 5:47 pm    Post subject: Re: Help with dynamic_cast Reply with quote



Hector wrote:

Quote:
I don't understand why this compiles

class Base {virtual int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

This compiles, because it is a good code.

Quote:
but the following doesn't compile

class Base {int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

In order to be used in dynamic_cast this way (to cast from base to
derived), the pointer (or reference) needs to point to the object of
polymorphic type.

The class is polymorphic, if it declares (or inherits) at least one
virtual function.

In your second example, neither Base nor Derived declare any virtual
function and from the compiler's viewpoint, they are not polymorphic, so
downcasts are not possible.

If your dummy function is not a good candidate to make it virtual for
some design reasons, a good solution is to make destructor virtual, even
if it would be an empty desctructor. In most cases (but prepare yourself
for the expectable following discussion that it is not always true nor
necessary), when using classes polymorphically, you want the destructor
in base class to be virtual anyway.

--
Maciej Sobczak
http://www.maciejsobczak.com/

Distributed programming lib for C, C++, Python & Tcl:
http://www.maciejsobczak.com/prog/yami/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Sat Aug 23, 2003 5:50 pm    Post subject: Re: Help with dynamic_cast Reply with quote



Hector wrote:
Quote:
I don't understand why this compiles

class Base {virtual int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

but the following doesn't compile

class Base {int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

You cannot use dynamic_cast with objects that don't have at least one
virtual function. This was a decision made to retain
backward-compatibility of the binary layout with C.

The implementation probably has a hidden virtual function that provides
info about the real type of the object. It then uses that info to
determine if and how the conversion from base to derived should take
place.

hth

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ralf Schneeweiß
Guest





PostPosted: Sat Aug 23, 2003 5:54 pm    Post subject: Re: Help with dynamic_cast Reply with quote



Hi,
the dynamic_cast<> operator needs a polymophic datatype to cast it.
It is using the v-table to improve the type and is giving back a 0-pointer
if the type doesn't fit. If there is no virtual function inside the
class-type,
there is also no v-table => the dynamic_cast<> cannot work.

Ralf Schneeweiß
http://www.oop-trainer.de


"Hector" <hhcalderon (AT) yahoo (DOT) com> schrieb im Newsbeitrag
news:e64dc7b5.0308222013.4861d376 (AT) posting (DOT) google.com...
Quote:
I don't understand why this compiles

class Base {virtual int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

but the following doesn't compile

class Base {int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

Is this a problem of my compiler or is there any general reason why it
shouldn't compile? Any help would be appreciated.

Hector C.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bronek Kozicki
Guest





PostPosted: Sat Aug 23, 2003 5:58 pm    Post subject: Re: Help with dynamic_cast Reply with quote

Quote:
class Base {int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

If you read compiler messages carefully, you will find out that object
b1 is not polymorphic. In other words, it does not have any runtime type
information. This information is not added to every object in your
program, as it would be unnecessary overhead (and there is a rule in C++
"don't pay for what you do not use" ). Only objects with virtual
functions table have runtime type information attached. In other words:
your base class must have at least one virtual function to be used
polymorphicaly.

BTW: following program has dangerous error:
Quote:

class Base {virtual int dummy(){}};

class Derived : public Base { };

Base* b1 = new Derived;
delete b1;
Quote:


..... because it's deleting object of class "Derived" through pointer to
its base class, and destructor being used is not virtual. Effectively
appropriate destructor (the one for Derived) will never be called. It's
undefined behaviour, ie. you may experience any kind of problems (from
"everything-seems-to-run-fine" during testing, through
"there-seems-to-be-some-memory-leak" bit later to
"hey-why-this-program-has-died" on customer desk)

When you are going to derive from some class, always make its destructor
virtual or protected. BTW: this is one of many reasons, why one should
not derive from most standard library classes.

Regards


B.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Sat Aug 23, 2003 8:27 pm    Post subject: Re: Help with dynamic_cast Reply with quote

[email]hhcalderon (AT) yahoo (DOT) com[/email] (Hector) writes:
[snip]
Quote:
class Base {virtual int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

but the following doesn't compile

class Base {int dummy(){}};
class Derived : public Base { };

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

Is this a problem of my compiler or is there any general reason why it
shouldn't compile? Any help would be appreciated.
[snip]


In C++, virtual and runtime polymorphism go hand in hand. Without
virtual you don't have runtime polymorphism (unless you implement
manually ...). You make a type polymorphic by declaring one its
member functions virtual. dynamic_cast is part of polymorphism.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sarat Venugopal
Guest





PostPosted: Tue Aug 26, 2003 6:44 pm    Post subject: Re: Help with dynamic_cast Reply with quote

Hi,

Quote:
Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

Is this a problem of my compiler or is there any general reason why it
shouldn't compile? Any help would be appreciated.

In C++, virtual and runtime polymorphism go hand in hand. Without
snip


Quote:
dynamic_cast is part of polymorphism.

That is misleading. dynamic_cast<> can exist without polymorphism. May be
more useful with polymorphism, but certainly not a requirement.

Base * b2 = dynamic_cast<Base*>(b1);

This is valid and gives expected results. This succeeds because
dynamic_cast<> is applied to the same type with the same level of
cv-qualification.

Another case:

D d2;
Base *b3 = dynamic_cast<Base*>(&d2);

Again you can cast to the base class subject to cv-qualificaltion levels
(5.2.7.5). So upcasting to a base class is allowed even for non-polymorphic
types.

However, any other cast requires a polymorphic expression. i.e even this
compiles (although the compiler knows this can never succeed) , since A is
polymorphic.

struct A { virtual ~A() {} };
A a;
Derived *d3 = dynamic_cast<Derived*>(&a);

Of course, the cast fails.

I am sure you knew this, but the way you put it seemed to suggest that
dynamic_cast *requires* a polymorphic expression.

Cheers,
Sarat Venugopal






[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Thu Aug 28, 2003 2:23 pm    Post subject: Re: Help with dynamic_cast Reply with quote

"Sarat Venugopal" <sarat_venugopal_no_spam (AT) msn (DOT) com> writes:

Quote:
Hi,

Base* b1 = new Derived;
Derived* d2 = dynamic_cast<Derived*>(b1);

Is this a problem of my compiler or is there any general reason why it
shouldn't compile? Any help would be appreciated.

In C++, virtual and runtime polymorphism go hand in hand. Without
snip

dynamic_cast is part of polymorphism.

That is misleading. dynamic_cast<> can exist without polymorphism. May be
more useful with polymorphism, but certainly not a requirement.
[snip]
I am sure you knew this, but the way you put it seemed to suggest that
dynamic_cast *requires* a polymorphic expression.

Yes, I did know this, but I didn't think of it because I don't often
use dynamic_cast<> for upcasts (unless the base is virtual).

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.