Keep in mind that this solution may lead to memory leak:
QFile *MyClass
::selectFile();
//...
QFile * file = selectFile
();
if (file) {
doSomethingWithFile(file);
delete file;
}
QFile *MyClass::selectFile();
//...
QFile * file = selectFile();
if (file) {
doSomethingWithFile(file);
delete file;
}
To copy to clipboard, switch view to plain text mode
If "dosomethingWithFile()" throws, control will never reach "delete file" and the resource will leak.
So I don't really understand why you won't use a QString object:
QStrign MyClass::selectFile();
//...
if (filePath.isEmpty() == false) {
...
}
QStrign MyClass::selectFile();
//...
QString filePath = selectFile();
if (filePath.isEmpty() == false) {
QFile file(filePath);
...
}
To copy to clipboard, switch view to plain text mode
Or at least use QFile pointer wrapped in smart pointer class, for example QSharedPointer<QFile>.
// edit:
btw. nDorian's code will cause a memory leak if selectFile() is called two or more times in a row without deleting "myFile" pointer in between. I know that this is forum section for "newbies" but this is important, any C++ programmer have to pay attention to such details - I know you are "not very experienced", but the sooner the better
Bookmarks