为什么这样做以及如何工作?非静态Qt对象

时间:2011-02-14 00:41:25

标签: c++ qt static qt4

我有点困惑,想清除它。

//QDir()::rmdir is from Qt Creator auto complete.
//It does not work. 
//Says no such static function.I looked it up, turns out to be true.
//Fair enough...though I'm not sure why auto-complete suggested it.
bool success = QDir()::rmdir("Y:/dir1/dir2/dir3");   //Does not work.

//Now I could make a QDir object as such.
//I didn;t test this but I'm sure it would work fine.
//However it seems clumsy.
QDir d("Y:/");     //This seems like a waste.
d.rmdir("Y:/dir1/dir2/dir3");

//Lastly, the source of my confusion.  QDir().rmdir
//This works, but WHY?
//There is no empty constructor for QDir in Qt Documentation.
//http://doc.qt.nokia.com/4.7/qdir.html
//Yet this empty constructor version works.  Why?
bool success = QDir().rmdir("Y:/dir1/dir2/dir3");

我主要担心的是为什么最后的examaple [QDir(。。rmdir)有效? 我在很多Qt课上都注意到了这一点。这是一个匿名对象,如果是这样 这对于物体清理意味着什么?这种形式是否安全使用?

2 个答案:

答案 0 :(得分:6)

其中一个QDir构造函数是:

QDir ( const QString & path = QString() )

您的QDir()。xxx代码正在调用此构造函数,然后使用默认值将QString()作为一个参数。

这样做是安全和正常的。

答案 1 :(得分:0)

QDir创建一个临时对象。如果你打电话,那就差不多了。喜欢:

QString s("123"); 
int answer = 40 + s.left(2).right(1).toInt();

第二行产生2个临时对象。

相关问题