PDA

View Full Version : What is the benefit of QT_BEGIN_NAMESPACE?



Spectralist
12th December 2009, 09:47
I've seen things like:

QT_BEGIN_NAMESPACE
class QPushButton;
QT_END_NAMESPACE
in a few samples. But as far as I can tell it just replaces #includeing the proper header. What's the benefit of using QT_BEGIN_NAMESPACE over #include?

Lykurg
12th December 2009, 10:26
As far as I know - right now - QT_BEGIN_NAMESPACE is not every time expanded. So normally it does nothing and therefor it is better since "class foo" is better than "#include foo" (forward declaration).
It is only expanded when you use Qt inside other namespaces and stuff like that...

squidge
12th December 2009, 10:40
Lykurg is correct, you can specify a namespace when you compile Qt, and these macros will use that namespace if its defined, or just expand to nothingness.

The rest is standard C++ - if you have no requirement to access the members of the object (QPushButton in this case), then using a forward reference is faster for the compiler to compile than #include'ing its header (apart from maybe when you use pre-compiled headers). If you look in the Qt source, forward declarations are used a lot so that an object doesn't include everything it depends on unless you really need it.