Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, September 2007
reinterpret_cast for d_ptr


Message 1 in thread

I was looking for the definition of Q_D macro used frequently in qt. And 
Q_D() macro is defined in qglobal.h header as:

#define Q_D(Class) Class##Private * const d = d_func()

And d_func() is:

#define Q_DECLARE_PRIVATE(Class) \
    inline Class##Private* d_func() { return reinterpret_cast(d_ptr); } \
    inline const Class##Private* d_func() const { return reinterpret_cast(d_ptr); } \
    friend class Class##Private;


if one look at the definition d_func(), could see the d_ptr variable. In QObject
source code d_ptr is simply a QObjectData type variable declared as a protected 
member of QObject class(qobject.h). Now when the above Q_D() macro  is used:

Q_D(SpinBox);

would be:

QSpinBoxPrivate *const d = d_func

And d_func() becomes:

inline QSpinBoxPrivate* d_func() { return reinterpret_cast(d_ptr); }

Now my question is why use reinterpret_cast inside the definition of d_func? I 
mean the safer static_cast could be used. Then why use something that is unsafe 
and not necessary? Is there any hidden reason that i'm not aware of?
Thanks. 

 
_________________________________________________________________
Explore the seven wonders of the world
http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE
--
 [ signature omitted ] 

Message 2 in thread

On 26.09.07 14:13:38, A D wrote:
> 
> I was looking for the definition of Q_D macro used frequently in qt. And 
> Q_D() macro is defined in qglobal.h header as:
> 
> #define Q_D(Class) Class##Private * const d = d_func()
> 
> And d_func() is:
> 
> #define Q_DECLARE_PRIVATE(Class) \
>     inline Class##Private* d_func() { return reinterpret_cast(d_ptr); } \
>     inline const Class##Private* d_func() const { return reinterpret_cast(d_ptr); } \
>     friend class Class##Private;
> 
> 
> if one look at the definition d_func(), could see the d_ptr variable. In QObject
> source code d_ptr is simply a QObjectData type variable declared as a protected 
> member of QObject class(qobject.h). Now when the above Q_D() macro  is used:
> 
> Q_D(SpinBox);
> 
> would be:
> 
> QSpinBoxPrivate *const d = d_func
> 
> And d_func() becomes:
> 
> inline QSpinBoxPrivate* d_func() { return reinterpret_cast(d_ptr); }
> 
> Now my question is why use reinterpret_cast inside the definition of d_func? I 
> mean the safer static_cast could be used. Then why use something that is unsafe 
> and not necessary? Is there any hidden reason that i'm not aware of?

Look into the archives, this exact same question was asked a few days
ago.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

>A D wrote:
> 
> I was looking for the definition of Q_D macro used frequently in qt. And 
> Q_D() macro is defined in qglobal.h header as:
> 
> #define Q_D(Class) Class##Private * const d = d_func()
> 
> And d_func() is:
> 
> #define Q_DECLARE_PRIVATE(Class) \
>     inline Class##Private* d_func() { return reinterpret_cast(d_ptr); } \
>     inline const Class##Private* d_func() const { return reinterpret_cast(d_ptr); } \
>     friend class Class##Private;
> 
> 
> if one look at the definition d_func(), could see the d_ptr variable. In QObject
> source code d_ptr is simply a QObjectData type variable declared as a protected 
> member of QObject class(qobject.h). Now when the above Q_D() macro  is used:
> 
> Q_D(SpinBox);
> 
> would be:
> 
> QSpinBoxPrivate *const d = d_func
> 
> And d_func() becomes:
> 
> inline QSpinBoxPrivate* d_func() { return reinterpret_cast(d_ptr); }
> 
> 
>Now my question is why use reinterpret_cast inside the definition of d_func? I 
>mean the safer static_cast could be used. Then why use something that is unsafe 
>and not necessary? Is there any hidden reason that i'm not aware of?
>>
>>Andreas wrote:
>>Look into the archives, this exact same question was asked a few days
>>ago.

Thanks Andreas. The problem is now resolved.
 

_________________________________________________________________
Connect to the next generation of MSN Messenger 
http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline
--
 [ signature omitted ]