Why does this work and how? Non-static Qt object
I'm a little confused and would like to clear this up.
//QDir()::rmdir is from Qt Creator auto complete.
//It does not w开发者_如何学JAVAork.
//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");
My main concern is why does the last examaple [QDir().rmdir] work? I've noticed this on a number of Qt classes. Is this an anonymous object and if so what does this mean with regards to object clean up? Is this form safe to use?
One of the QDir constructors is:
QDir ( const QString & path = QString() )
Your QDir().xxx code is calling this constructor, which then uses default to using a QString() as one argument.
This is safe and normal to do.
QDir creates a temporary object. Pretty the same if you callsth. like:
QString s("123");
int answer = 40 + s.left(2).right(1).toInt();
Second line produces 2 temporary objects.
精彩评论