[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: MORE INFO (Re: What is the difference between dynamic and static_cast)
From:       Guillaume Laurent <glaurent () telegraph-road ! org>
Date:       2000-11-09 9:35:47
[Download RAW message or body]

On Thursday 09 November 2000 08:33, Thomas Leitner wrote:
> O.k. I was talking to my Compaq C++ contact and he came up with this below
> (it's a reply from me to him, to avoid confusion) The main question seems
> to be:
>
> Is it legal to dynamic_cast from a base_class* to a derived_class* ?

Yes. That's the whole purpose of dynamic_cast<> : being able to do such a 
cast *safely*. That is, you can check if it's valid or not.

> On Wed, 8 Nov 2000 cxxc_bugs@cxxc.zko.dec.com wrote:
> > void KMFolderTree::doFolderSelected( QListViewItem* qlvi )
> > {
> > [...]
> > struct Qt {};
> > struct QListViewItem: public Qt
> > {
> >     virtual ~QListViewItem() {;}
> > };
> > class KMFolderTreeItem : public QListViewItem {};
> > class  QCheckListItem : public QListViewItem {};
> >
> > If qlvi is of type QListViewItem* then I would expect the cast
> > to fail because you are trying to cast a Base* to a Derived*
> > not vice versa.

This answer is quite misleading. Casting a Base* to Derived* is absolutely 
legal. Period. However it can fail if Base* points to an actual Base object, 
and not on a Derived object. That's how I understand what he says here.

You have another way to check : check what className() returns. Qt has its 
own RTTI system, might as well use it.

-- 
						Guillaume
						http://www.telegraph-road.org
 
>> Visit http://master.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic