我有些困惑,想解决这个问题。
//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");
我主要关心的是为什么最后一个示例[QDir()。rmdir]起作用?
我在许多Qt类上都注意到了这一点。这是一个匿名对象吗?
关于对象清理,这意味着什么?这种表格使用安全吗?
最佳答案
QDir构造函数之一是:
QDir ( const QString & path = QString() )
您的QDir()。xxx代码正在调用此构造函数,然后使用默认构造函数将QString()用作一个参数。
这是安全且正常的。
关于c++ - 为什么这样做,如何运作?非静态Qt对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4987955/