I'm trying to set a permission for a file. I thought I could save a line of code while dealing with a QFile
object, like so.
QFile("somefile.txt").setPermissions(QFile::WriteOther);
It compiled and ran, but didn't do anything. Of course, when I did it the right way, it worked. (no surprise, there.)
QFile tempFileHandle("somefile.txt");
tempFileHandle.setPermissions(QFile::WriteOther);
I think this is a good opportunity to understand the C++ syntax. I'll accept that my original way doesn't work, but why?
Well, I don't know QFile and don't know exactly what your observation is but it probably boils down to whatever is done in QFile's destructor.
The first example creates temporary object. I guess its constructor creates somefile.txt. Then setPermissions sets whatever you specified on that file. Now the question is what destructor does:
In the other example you create named variable which is not destroyed until it goes out of scope and you probably can even detach the object from the file on disk which will probably nullify destructor effects on that file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With