PDA

View Full Version : Strange behavior of Q_DECLARE_PRIVATE_D() macro



Scope
22nd February 2016, 15:57
Hi, I found strange behavior of Q_DECLARE_PRIVATE_D macro

For example when i declare:


QScopedPointer<MetaDataPrivate> d_ptr;
Q_DECLARE_PRIVATE(MetaData)


then it works fine but when I try use Q_DECLARE_PRIVATE_D macro with second parameter for name of my pointer


QScopedPointer<MetaDataPrivate> d;
Q_DECLARE_PRIVATE_D(d, MetaData)


I get error:

error: invalid cast from type 'QScopedPointer<MetaDataPrivate>' to type 'MetaDataPrivate*'

Maybe someone know what I am doing wrong here? How I can use Q_DECLARE_PRIVATE_D with my name of pointer.

Regards,

anda_skoa
22nd February 2016, 16:10
maybe


QScopedPointer<MetaDataPrivate> d;
Q_DECLARE_PRIVATE_D(d.data(), MetaData)


Cheers,
_

Scope
22nd February 2016, 16:33
These macros are different:



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

#define Q_DECLARE_PRIVATE_D(Dptr, Class) \
inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(Dptr); } \
inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(Dptr); } \
friend class Class##Private;


diferrent part

Q_DECLARE_PRIVATE

(qGetPtrHelper(d_ptr));
Q_DECLARE_PRIVATE_D

(Dptr);

but I really do not know why... Maybe it is bug in Qt?
I think that Q_DECLARE_PRIVATE_D macro should work with QScopedPointer similar as Q_DECLARE_PRIVATE macro.

Added after 13 minutes:

anda_skoa yes, it works but i wonder why they changed Q_DECLARE_PRIVATE_D macro and we can not pass QScopedPointer directly like in Q_DECLARE_PRIVATE.

anda_skoa
22nd February 2016, 18:42
The missing piece is likely that qGetPtrHelper() function, it must have overloads for raw pointer, scoped pointer and maybe others.

You could test that theory, i.e. use qGetPtrHelper(Dptr) inside the cast parentheses and submit a patch on Qt's gerrit if it works.

Cheers,
_