【问题标题】:C++, namespaces, name lookup on inherited class and QtC++、命名空间、继承类和 Qt 上的名称查找
【发布时间】:2014-06-26 10:03:59
【问题描述】:

我已经使用 QT_NAMESPACE=Qt_ns 选项构建了 Qt5。

以下代码编译成功。但为什么呢?

QT_BEGIN_NAMESPACE
namespace Ui {
  class MainWindow;
}
QT_END_NAMESPACE

namespace test_ns
{
class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

private:
  Qt_ns::Ui::MainWindow *ui;
};    
}

QMainWindow 被包装在命名空间 Qt_ns 中,所以我认为有必要在此名称前加上 Qt_ns 前缀

class MainWindow : public Qt_ns::QMainWindow

我徒劳地用谷歌搜索,阅读 C++ 标准无济于事。 我不认为 ADL 在这里适用,因为它适用于函数、错误、参数。 我在 QMainWindow 上搜索了 Qt 源代码,但我也迷路了。

所以,请转给你,扩展我的知识。

【问题讨论】:

    标签: c++ c++11 qt


    【解决方案1】:

    检查qglobal.h,当QT_NO_USING_NAMESPACE 未定义时,您将看到QT_USE_NAMESPACE。附上以下评论:

    # ifndef QT_NO_USING_NAMESPACE
       /*
        This expands to a "using QT_NAMESPACE" also in _header files_.
        It is the only way the feature can be used without too much
        pain, but if people _really_ do not want it they can add
        DEFINES += QT_NO_USING_NAMESPACE to their .pro files.
        */
       QT_USE_NAMESPACE
    # endif
    

    这允许在不使用命名空间的情况下直接使用QMainWindow

    【讨论】:

    • 我真的打败了你。我在 Qt 源中搜索了 QT_BEGIN_NAMESPACE 并查看了所有命名空间的内容。
    猜你喜欢
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 2011-05-29
    • 2013-03-25
    • 2013-03-10
    • 2017-09-27
    • 2015-07-18
    • 2011-06-28
    相关资源
    最近更新 更多