Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting classes inside namespaces

I see that Qt puts a class inside the Ui interface like this:

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    ...

Is this method the same as enclosing the entire class inside the namespace? It certainly looks cleaner.

like image 231
wrongusername Avatar asked Dec 29 '22 03:12

wrongusername


1 Answers

No, in your example Ui::MainWindow is a different class than the MainWindow class defined in the global namespace.

Behold:

namespace Ui {
    class MainWindow;
}

class MainWindow
{

};

int main()
{
    Ui::MainWindow mw; // fails due to incomplete type
}

This code won't compile, since Ui::MainWindow is an incomplete type.

Most likely, the Qt code was just using a forward declaration. You can forward declare the class in a namespace, but you still must actually implement the class in the same namespace, or else it is not the same class.

like image 112
Charles Salvia Avatar answered Jan 13 '23 19:01

Charles Salvia