QT_BEGIN_NAMESPACE



  • Hi, I have some dubts on the use of namespace.
    In an example I found this code for include this Class:

    QT_BEGIN_NAMESPACE

    class QLabel;

    class QPushButton;

    class QTextEdit;

    class QLineEdit;

    QT_END_NAMESPACE

    Then I tried to do the same thing, but didn't work,
    so I used
    #include <QPushButton> in place of
    QT_BEGIN_NAMESPACE

    class QPushButton;

    QT_END_NAMESPACE

    Do someone know why? THANKS.



  • Class.h
    @
    QT_BEGIN_NAMESPACE

    class QLabel;
    class QPushButton;
    class QTextEdit;
    class QLineEdit;

    QT_END_NAMESPACE@

    class.cpp
    @#include <QPushButton>
    #include <QLabel>
    #include <QTextEdit>
    #include <QLineEdit>@


  • Moderators

    @class QPushButton;@ is a forward declaration, which only tells the compiler what type QPushButton is, but doesn't tell the compiler what its methods/variables/etc. are.

    @#include <QPushButton>@ inserts the full header file of QPushButton into your code, so that the compiler knows what its methods/variables/etc. are.

    See http://stackoverflow.com/questions/553682/when-to-use-forward-declaration for tips on when to use forward declarations


  • Moderators

    For those who are interested in the meaning of "QT_BEGIN_NAMESPACE and QT_END_NAMESPACE, here":http://qt-project.org/wiki/Qt-In-Namespace are the details.



  • Guys, I was wondering about using this little macro "trick". I have a namespace for most of my code too, but, when should I use a XXX_BEGIN_NAMESPACE ... XXX_END_NAMESPACE, and when is it more appropriate a using nampesace XXX ?
    My understanding is that the first one is more appropriate for header files (.h) where most of the classes and global object' declarations go; while in source code files (.cpp) the second is more appropriate. Do you agree?


  • Moderators

    @T3STY said:

    Guys, I was wondering about using this little macro "trick". I have a namespace for most of my code too, but, when should I use a XXX_BEGIN_NAMESPACE ... XXX_END_NAMESPACE, and when is it more appropriate a using nampesace XXX ?
    My understanding is that the first one is more appropriate for header files (.h) where most of the classes and global object' declarations go; while in source code files (.cpp) the second is more appropriate. Do you agree?

    I don't recommend the macro approach; it just makes things messier. Qt uses it to provide flexibility: Users can specify the namespace (OR specify no namespace) to wrap Qt classes, when the Qt libraries are being compiled. Your project is unlikely to need this.

    Be careful if you use using namespace XXX, as it can defeat the purpose of having a namespace: http://julipedia.meroh.net/2013/12/header-files-avoid-c-using-directives.html

    The best approach is to just use prefix your classes/functions/etc with the namespace name.



  • Got it, thank you very much :)
    (nice website you linked, btw)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.