I have a class which will parse an XML file and return data based on content and what methods are used. I wish to be able to initiate the object using either a file name with path or a pointer to an already open file.
If the pointer to an already opened file is given, then it runs the constructor and thats it. If the file name is passed, then that constructor will open the file, then pass the address to the opened file to the second constructor.
//foo.h
class foo
{
public:
foo(const QString fileName);
foo(QFile *fp);
...
...
}
//foo.cpp
class foo::foo(const QString fileName)
{
if(fileName.isEmpty()) {
// Return error.
} else {
// Open file and pass the address to the second constructor.
fp = new QFile(fileName);
fp->open(...);
foo::foo(fp); // Execute second constructor.
}
}
class foo::foo(QFile *fp)
{
if(fp == NULL) {
// Return error.
} else {
// Do stuff with open file and further initiate the object.
}
}
This was my first idea on how to do this, but I feel I am over complicating the issue. Is this an OK way of doing this, is it sane and is there a best practice for this type of situation?