問題描述
我剛遇到這個問題
error: request for member ‘show’ in ‘myWindow’, which is of non-class type ‘MainGUIWindow()’
嘗試編譯一個簡單的 qt 應用程序時:
when trying to compile a simple qt-application:
#include <QApplication>
#include "gui/MainGUIWindow.h"
int main( int argc, char** argv )
{
QApplication app( argc, argv );
MainGUIWindow myWindow();
myWindow.show();
return app.exec();
}
我通過替換
MainGUIWindow myWindow();
由
MainGUIWindow myWindow;
但我不明白其中的區別.我的問題:有什么區別?
but I don't understand the difference. My question: What is the difference?
問候,德克
推薦答案
其他答案正確地指出括號版本實際上是一個函數聲明.為了直觀地理解它,假設你寫的 MainGUIWindow f();
看起來更像一個函數,不是嗎?:)更有趣的問題是
The other answers correctly state that the parentheses version is actually a function declaration. To understand it intuitively, suppose you wrote MainGUIWindow f();
Looks more like a function, doesn't it? :)
The more interesting question is what is the difference between
MainGUIWindow* p = new MainGUIWindow;
和
MainGUIWindow* p = new MainGUIWindow();
帶括號的版本稱為值初始化,而沒有括號的版本稱為默認初始化.對于非 POD 類,兩者沒有區別.然而,對于 POD 結構,值初始化涉及將所有成員設置為 0,
The version with parentheses is called value-initialization, whereas the version without is called default-initialization. For non-POD classes there is no difference between the two. For POD-structs, however, value-initialization involves setting all members to 0,
my2c
補充:一般來說,如果某個句法結構既可以解釋為聲明又可以解釋為其他東西,編譯器總是會以聲明的方式解決歧義.
Addition: In general, if some syntactic construct can be interpreted both as a declaration and something else, the compiler always resolves the ambiguity in favor of the declaration.
這篇關于使用 () 或不使用 () 創建對象的區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!