In cpp,
I know &
is used to get the address
and ::
is used to get the member of class,
but, what's the usage of &::
?
Here is the example of the function connect
of qt5:
QtWidgetsApplication2::QtWidgetsApplication2(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.pushButton_select, &QPushButton::clicked,this,&::QtWidgetsApplication2::select_file);
}
void QtWidgetsApplication2::select_file()
{
file_path = QFileDialog::getOpenFileName(NULL, QStringLiteral("选择文件"), "D:", QStringLiteral("音频文件(*wav)"));
}
what's the meaning of &::
in this line :
connect(ui.pushButton_select, &QPushButton::clicked,this,&::QtWidgetsApplication2::select_file);
why not &QtWidgetsApplication2::select_file
?
CodePudding user response:
::
is used to get the member of class
While this is a correct assumption it is not limited to just class. This operator give access to a scope. And when it has no identifiers on its left side it explicitly tells compiler to use the global scope to resolve the given name. If we look at the operator precedence order the scope resolution operator ::
is on the top of the list, so it will be resolved first, then the address of operator &
.
So if you would put parenthesis, your code should read as & ( (::QtWidgetsApplication2) ::select_file)
.
CodePudding user response:
what's the meaning of
&::
in this line :
It essentially means that the topmost scope(occuring at the leftmost side) is the global scope. That is, we start by looking into the global namespace first.
why not
&QtWidgetsApplication2::select_file
?
This would mean that we start by looking into QtWidetsApplication2
first instead of the global namespace.
Consider the following contrived example:
namespace P
{
struct C
{
constexpr static int i = 0;
};
}
struct C
{
constexpr static int i = 0;
};
//-------------vvv-------->uses global class C
const int *j = &::C::i;
//-------------vv-------->uses class C in namespace P
const int *k = &P::C::i;
CodePudding user response:
The & operator is used in C to take the address of a variable or function. In this case, &QPushButton::clicked is taking the address of the clicked member function of the QPushButton class.
The :: operator is used in C to specify a member of a class, namespace, or enumeration. In this case, QPushButton::clicked is specifying the clicked member of the QPushButton class.
The combination &:: is used to take the address of a global function or static member function. In this case, &::QtWidgetsApplication2::select_file is taking the address of the select_file static member function of the QtWidgetsApplication2 class. This is necessary because the connect function expects the address of the function to be called when the signal is emitted.
It's also worth noting that you could write the same code using the std::bind function instead, like this:
connect(ui.pushButton_select, &QPushButton::clicked, std::bind(&QtWidgetsApplication2::select_file, this));
This has the same effect as the original code, but it uses the std::bind function to bind the this pointer to the select_file member function, allowing it to be called as a regular function rather than using the &:: syntax.
CodePudding user response:
In this case, the "&" symbol is used to take the address of a function, rather than a variable. This is often used in C when connecting signals to slots in the Qt framework. In this specific example, the "&QPushButton::clicked" part of the code is taking the address of the "clicked" signal in the QPushButton class, and the "&::QtWidgetsApplication2::select_file" part is taking the address of the "select_file" member function in the "QtWidgetsApplication2" class.