(Qt Creator - 基于 Cpp 的应用程序)QVS 使用指针?
这是我的代码:
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QTextEdit *textEdit;
};
#endif // MAINWINDOW_H
// MainWindow.cpp
#include "mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
{
textEdit = new QTextEdit();
}
MainWindow::~MainWindow()
{
delete textEdit;
}
//main.cpp
#include <QtGui>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
它是否更有效(这是问题的“Q[Objects] VS使用指针?”部分):
1)像我实际做的那样使用指针或
2)使用对象(删除* +删除语句)
谢谢你!
here is my code:
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QTextEdit *textEdit;
};
#endif // MAINWINDOW_H
// MainWindow.cpp
#include "mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
{
textEdit = new QTextEdit();
}
MainWindow::~MainWindow()
{
delete textEdit;
}
//main.cpp
#include <QtGui>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Is it more efficient (here's the "Q[Objects] VS using Pointers?" part of the question) to:
1) Use pointers as I am actually doing or
2) Use objects (removing * + delete statement)
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于作为指针的
QObject
成员,您不应使用delete
,QTextEdit
可能是MainWindow
的子级,所以它会被自动删除。当然,理论上,使用非指针 QObject 成员会更快,并且间接层数少一级。像这样(对于那些不理解问题的人):
并且需要输入的代码也更少,因为您不必重新输入成员的类名来在构造函数中初始化它们。
但由于
QObject
本身已经大量使用间接(及其 d 指针),增益可能可以忽略不计。使用指针成员键入的额外代码可以让您在之间较低耦合你的头文件和 Qt,因为你可以使用前向声明而不是头包含,这意味着更快的编译(特别是如果你还没有使用预编译头)和重新编译。此外,
如果您没有按照正确的顺序分别删除/声明它们, 可能会导致双重删除(孩子然后父母删除,或者父母然后孩子声明)。
例如:
当
MainWindow
被删除时,它的非静态类成员按照它们在类中声明的相反顺序被删除。所以scrollArea
首先被销毁,然后是textEdit
,但是scrollArea
也销毁了它的子级,包括textEdit
,所以textEdit
被有效地破坏了两次,导致崩溃。因此,使用
QObject
成员作为指针,并且不删除自己有父级的QObject
,更不容易出错。For
QObject
members as pointers, you shouldn't usedelete
, theQTextEdit
will probably be a child ofMainWindow
, so it will be deleted automatically.It would, of course, be theoretically faster to use non-pointer
QObject
members, with one less level of indirection. Like this (for those who didn't understand the question):and there is also less code to type, because you don't have to retype the class name of the members to initialize them in the constructor.
But since
QObject
are themselves already heavily using indirection (with their d-pointer), the gain will probably be negligible. And the extra code you type with pointer members allows you to have a lower coupling between your header files and Qt, because you can use forward declarations instead of header inclusion, which means faster compilations (especially if you are not using precompiled headers yet) and recompilations.Also,
QObject
pointer members, orQObject
as non-pointers memberscan causes double deletion, if you don't respectively delete/declare them in the right order (children then parents for deletion, or parents then children for declaration).
For example:
When
MainWindow
is deleted, its non-static class members are deleted in the reverse order of their declaration in the class. SoscrollArea
is destroyed first thentextEdit
, butscrollArea
also destroys its children includingtextEdit
, sotextEdit
is effectively destroyed twice causing a crash.So it is less error prone to use
QObject
members as pointers, and to not delete theQObject
which have a parent yourself.尝试在堆栈上创建 QLineEdit,然后将其放入布局...退出应用程序...您看到什么?提示:在调试器中启动您的应用程序。
ksming 关于阅读文档的说法是正确的。这不是特定于语言的问题。如果您问哪个更快:堆分配或堆栈分配,那么请正确地提出您的问题。
Try creating QLineEdit on the stack and then put it into layout... Quit your application... What do you see? HINT: launch your application in debugger.
ksming is right about reading documentation. It is not language specific issue. If you are asking what is faster: heap or stack allocation then form your question properly.