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

List:       kde-core-devel
Subject:    Re: Is this portable?
From:       Reginald Stadlbauer <reggie () trolltech ! com>
Date:       2000-04-08 14:05:54
[Download RAW message or body]

On Sat, 08 Apr 2000, mosfet wrote:
> Reginald Stadlbauer wrote:
> > On Sat, 08 Apr 2000, mosfet wrote:
> > > Hi all,
> > >
> > > I got some code where I need to cast a list of QObject pointers to
> > > QWidgets. Some of the QObjects are actually QWidgets, some are not. I
> > > know it is legal to do something like:
> > >
> > > QWidget *widget = (QWidget *)someQObjectPointer;
> > >
> > > because QWidget derives from QObject.
> > >
> > > My question is, on my gcc at least if the object isn't a QWidget but a
> > > QObject the resulting pointer from the cast will be NULL. Is this the
> > > same on all compilers, or on some systems does it generate an invalid
> > > pointer? I don't know if there is a standard behavior for this.
> >
> > Can't you do a
> >
> > if ( object->isWidgetType() )
> >
> > or
> >
> > if ( object->inherits( "QWidget" ) )
> >
> > before doing the cast. That should be save in any way. Or use
> > dynamic_cast.
>
> Yeah, inherits() is what I usually use, but this is in a painting
> operation and
> I want it to be as fast as possible ;-) I figure a normal cast and just
> checking if it's NULL would be the fastest way, but I don't know about
> portability...

I just tried that on the SGI (with IRIX CC) which is sitting next to me, and 
there it doesn't work, you get back a "valid" painter - means not a NULL 
pointer. With dynamic_cast it works fine. 

But, using QObject::isWidgetType() is fast:

    bool	 isWidgetType()	  const { return isWidget; }
 
So, this should be ok for you.

-- 
Reggie (reggie@trolltech.com)

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

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