Basic knowledge
Qt knowledge points combing - NameSpace namespace
Article directory
namespace role
standard namespace
use namespace
Qt custom namespace
Project source code
development environment
namespace role
Namespace is a scope introduced by ANSIC++ that can be named by users to deal with common name conflicts in programs;
Such as solving the problem: [Exception] Indirect addressing levels are different
In C++, namespaces are declared using namespace, and { } is used to delimit the scope of the namespace, for example:
namespace func{
int age=18;
}
standard namespace
The standard namespace std in C++, std is the abbreviation of standard, which means "standard namespace"; the functions or objects in the C++ standard library are defined in the namespace std;
Qt has its own namespace: you can see it when you create a new project with a form;
namespace Ui { class MainWindow; }
use namespace
1. Refer to the namespace in the program head, such as:
using namespace std;
2. Add namespaces when referencing variables or methods, such as:
SpaceA::ToolA::Func1();
Qt custom namespace
1. Use namespace and { } to define the scope of the namespace;
2. Write a variable method;
3. Use using namespace xxx to refer to this namespace when referencing;
An example is as follows:
ToolA.h
#ifndef TOOLA_H
#define TOOLA_H
#include <QObject>
namespace SpaceA {
class ToolA : public QObject
{
Q_OBJECT
public:
explicit ToolA(QObject *parent = nullptr);
static void Func1();
static void Func2();
static void Func3();
static void Func4();
};//class
}//namespace
#endif // TOOLA_H
ToolA.cpp
#include "toola.h"
#include <QDebug>
namespace SpaceA {
ToolA::ToolA(QObject *parent) : QObject(parent)
{
}//class
void ToolA::Func1()
{
qDebug() << "ToolA Func1";
}
void ToolA::Func2()
{
qDebug() << "ToolA Func2";
}
void ToolA::Func3()
{
qDebug() << "ToolA Func3";
}
void ToolA::Func4()
{
qDebug() << "ToolA Func4";
}
}//namespace
Referenced in MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
//using namespace SpaceA; //There is no need to add SpaceA before each call after the namespace is quoted here
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
SpaceA::ToolA::Func1();//After the header refers to the namespace, there is no need to add SpaceA before each call
SpaceA::ToolA::Func2();
SpaceA::ToolA::Func3();
SpaceA::ToolA::Func4();
ToolB::Func1();
ToolB::Func2();
ToolB::Func3();
ToolB::Func4();
}
MainWindow::~MainWindow()
{
delete ui;
}
Project source code
Project Git address: lizhifun/QtNamespaceDemo
development environment
Author: Lizhifun
OS: Windows 10 Home Chinese
Compiler: Microsoft Visual C++ Compiler 15.9.28307.1259 (amd64)
Kit: Desktop Qt 5.14.2 MSVC2017 64bit
Qt Creator: 4.11.1
———————————————
Copyright statement: This article is an original article by CSDN blogger "One Little Fenghua", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/tingzhiyi/article/details/112584777