PDA

View Full Version : Dereferencing a NULL Pointer for staticMetaObject?



hyling
14th December 2006, 23:51
Hi,

I'm stumped... how does this work? This piece of code is from qobject.h in the function qFindChildren. It looks like it dereferences a NULL pointer to get the staticMetaObject?? :eek:


reinterpret_cast<T>(0)->staticMetaObject

Thanks!
Hua-Ying

jacek
15th December 2006, 00:29
Well... not exactly, staticMetaObject is a static member, so you don't have to dereference anything to access it --- you just have to know the correct type. Before static methods were introduced into C++, that was a way to simulate them and in fact it's an equivalent of:
T::staticMetaObject
I'm not sure why it was written that way, but it might have something to do with some limitations of one of the compilers Qt supports or maybe it allows compiler to do some optimizations.