I've been looking at a Qt tutorial which uses a construction I haven't seen before:
我一直在看一个Qt教程,它使用了我以前没见过的结构:
(void) new QShortcut(Qt::Key_Enter, this, SLOT(fire()));
(void) new QShortcut(Qt::Key_Return, this, SLOT(fire()));
(void) new QShortcut(Qt::CTRL + Qt::Key_Q, this, SLOT(close()));
I've tried this without the (void)
and it still compiles and works, so what is the purpose of the (void)
?
我已经尝试了这个没有(void)但它仍然编译和工作,那么(void)的目的是什么?
3 个解决方案
#1
Casting an expression to (void) basically tells the compiler to ignore the result of that expression (after computing it).
将表达式转换为(void)基本上告诉编译器忽略该表达式的结果(在计算之后)。
In your example, each of the expressions (per statement/line) is dynamically allocating memory via the new operator - and since new returns a pointer (address) to the memory, the usual practice is to store that pointer in a variable that you can use to eventually delete the object and deallocate the memory. In your example, since the pointer is being discarded, an explicit cast to (void) makes the intention of the programmer clear: "I know exactly what I am doing - please discard the value returned by new"
在您的示例中,每个表达式(每个语句/行)都是通过new运算符动态分配内存 - 并且由于new将指针(地址)返回到内存,通常的做法是将该指针存储在您可以使用的变量中用于最终删除对象并释放内存。在您的示例中,由于指针被丢弃,显式强制转换为(void)使程序员的意图明确:“我确切地知道我在做什么 - 请丢弃新返回的值”
If you are interested in the technicalities (quoting from the C++ standard, clause 5):
如果您对技术问题感兴趣(引用C ++标准,第5条):
Any expression can be explicitly converted to type cv void. The expression value is discarded. [ Note: however, if the value is in a temporary variable (12.2), the destructor for that variable is not executed until the usual time, and the value of the variable is preserved for the purpose of executing the destructor. —end note ]
The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are not applied to the expression.任何表达式都可以显式转换为cv void类型。表达式值被丢弃。 [注意:但是,如果值在临时变量(12.2)中,则该变量的析构函数在正常时间之前不会执行,并且为了执行析构函数而保留变量的值。 -end note]左值到右值(4.1),数组到指针(4.2)和函数到指针(4.3)标准转换不应用于表达式。
And if you are wondering how those objects are getting deleted (if indeed they are getting deleted), the answer is that the 'this' pointer within the constructor of QShortcut should have the same value as that returned by new, and that can be passed to a ShortcutManager. Note, the 'this' within the constructor is not the same as the 'this' pointer that is being passed to the constructor of QShortcut.
如果你想知道这些对象是如何被删除的(如果它们确实被删除了),答案是QShortcut的构造函数中的'this'指针应该与new返回的值相同,并且可以传递到一个ShortcutManager。注意,构造函数中的'this'与传递给QShortcut的构造函数的'this'指针不同。
#2
Some C++ compilers will give a warning if you throw away the return value of a function like that. In this case, the code looks like it leaks memory, so you get a warning. The (void)
tells the compiler not to emit the warning: "Treat this function as though it returned void
".
如果你丢弃像这样的函数的返回值,一些C ++编译器会发出警告。在这种情况下,代码看起来像是泄漏内存,因此您会收到警告。 (void)告诉编译器不要发出警告:“将此函数视为返回void”。
#3
I think the reference returned from the new
statement isn't assigned to a pointer. I see that this
is being passed so QT must do something with that reference.
我认为从新语句返回的引用未分配给指针。我知道这是通过的,因此QT必须对该引用做一些事情。
#1
Casting an expression to (void) basically tells the compiler to ignore the result of that expression (after computing it).
将表达式转换为(void)基本上告诉编译器忽略该表达式的结果(在计算之后)。
In your example, each of the expressions (per statement/line) is dynamically allocating memory via the new operator - and since new returns a pointer (address) to the memory, the usual practice is to store that pointer in a variable that you can use to eventually delete the object and deallocate the memory. In your example, since the pointer is being discarded, an explicit cast to (void) makes the intention of the programmer clear: "I know exactly what I am doing - please discard the value returned by new"
在您的示例中,每个表达式(每个语句/行)都是通过new运算符动态分配内存 - 并且由于new将指针(地址)返回到内存,通常的做法是将该指针存储在您可以使用的变量中用于最终删除对象并释放内存。在您的示例中,由于指针被丢弃,显式强制转换为(void)使程序员的意图明确:“我确切地知道我在做什么 - 请丢弃新返回的值”
If you are interested in the technicalities (quoting from the C++ standard, clause 5):
如果您对技术问题感兴趣(引用C ++标准,第5条):
Any expression can be explicitly converted to type cv void. The expression value is discarded. [ Note: however, if the value is in a temporary variable (12.2), the destructor for that variable is not executed until the usual time, and the value of the variable is preserved for the purpose of executing the destructor. —end note ]
The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are not applied to the expression.任何表达式都可以显式转换为cv void类型。表达式值被丢弃。 [注意:但是,如果值在临时变量(12.2)中,则该变量的析构函数在正常时间之前不会执行,并且为了执行析构函数而保留变量的值。 -end note]左值到右值(4.1),数组到指针(4.2)和函数到指针(4.3)标准转换不应用于表达式。
And if you are wondering how those objects are getting deleted (if indeed they are getting deleted), the answer is that the 'this' pointer within the constructor of QShortcut should have the same value as that returned by new, and that can be passed to a ShortcutManager. Note, the 'this' within the constructor is not the same as the 'this' pointer that is being passed to the constructor of QShortcut.
如果你想知道这些对象是如何被删除的(如果它们确实被删除了),答案是QShortcut的构造函数中的'this'指针应该与new返回的值相同,并且可以传递到一个ShortcutManager。注意,构造函数中的'this'与传递给QShortcut的构造函数的'this'指针不同。
#2
Some C++ compilers will give a warning if you throw away the return value of a function like that. In this case, the code looks like it leaks memory, so you get a warning. The (void)
tells the compiler not to emit the warning: "Treat this function as though it returned void
".
如果你丢弃像这样的函数的返回值,一些C ++编译器会发出警告。在这种情况下,代码看起来像是泄漏内存,因此您会收到警告。 (void)告诉编译器不要发出警告:“将此函数视为返回void”。
#3
I think the reference returned from the new
statement isn't assigned to a pointer. I see that this
is being passed so QT must do something with that reference.
我认为从新语句返回的引用未分配给指针。我知道这是通过的,因此QT必须对该引用做一些事情。