A
Anton Pervukhin
Hi everybody!
I have a small problem regarding the initialization of pointer to the
file stream under some conditions.
Imagine a class which has a pointer to output file stream and some
additinional methods to deal with it, i.e. open/close/write:
classA {
private:
const std::auto_ptr<std:fstream> filePtr;
std::_Ios_Openmode mode;
public:
classA(const std::string& name, bool isAppended = true);
virtual ~ClassA{};
void setAppended(bool isAppended);
bool isAppended();
bool reopen();
void close();
}
The usage of std:fstream in my case is well suited to be defined as
auto_ptr.
There is also a variable "mode" which defines in which mode file should
be opened (i.e. appended, truncated etc)
Now I'm trying to write a constructor to set both private variables.
Clearly I would like to open "filePtr" with the mode given by
"isAppended" formal variable.
classA::ClassA(const std::string& fileName, bool isAppended) {
this->setAppended(isAppended); // to set
mode
*filePtr = *(new std:fstream(fileName.c_str(), mode)); //
Compile error
}
which leads to a compilation error that std::ios_base:perator= is
private.
I could set filePtr in initialization list like this:
ClassA::ClassA(const std::string& fileName, bool isAppended):
filePtr(new std:fstream(fileName.c_str())){
this->setAppended(isAppended);
}
but then at the moment of "filePtr" initialization its "mode" is not
known yet, meaning that "filePtr" should be reopened again later with
the right "mode".
Putting initialization of "mode" in a list before "filePtr" doesn't
guarantied that it will be initialized before.
There is a solution to make function "setAppended" returns bool and put
it this way:
ClassA::ClassA(const std::string& fileName, bool isAppended):
filePtr(new std:fstream(fileName.c_str(),
this->setAppended(isAppended))){
}
which looks rather ugly... Putting in the same place the code of
function "setAppended" instead of calling it, duplicates the code.
So all in all I'm wondering whether "filePtr" could be initialized
somehow but in the constructor body and not its initialization list.
the case with using reference like this...
///
std:fstream& fileRef;
....
std:fstream tempRef(fileName.c_str(), mode);
fileRef = tempRef;
///
...won't work because I cannot define reference for auto_ptr like this:
const std::auto_ptr<std:fstream&> filePtr
Any tips will be appreciated!
regards,
Anton
I have a small problem regarding the initialization of pointer to the
file stream under some conditions.
Imagine a class which has a pointer to output file stream and some
additinional methods to deal with it, i.e. open/close/write:
classA {
private:
const std::auto_ptr<std:fstream> filePtr;
std::_Ios_Openmode mode;
public:
classA(const std::string& name, bool isAppended = true);
virtual ~ClassA{};
void setAppended(bool isAppended);
bool isAppended();
bool reopen();
void close();
}
The usage of std:fstream in my case is well suited to be defined as
auto_ptr.
There is also a variable "mode" which defines in which mode file should
be opened (i.e. appended, truncated etc)
Now I'm trying to write a constructor to set both private variables.
Clearly I would like to open "filePtr" with the mode given by
"isAppended" formal variable.
classA::ClassA(const std::string& fileName, bool isAppended) {
this->setAppended(isAppended); // to set
mode
*filePtr = *(new std:fstream(fileName.c_str(), mode)); //
Compile error
}
which leads to a compilation error that std::ios_base:perator= is
private.
I could set filePtr in initialization list like this:
ClassA::ClassA(const std::string& fileName, bool isAppended):
filePtr(new std:fstream(fileName.c_str())){
this->setAppended(isAppended);
}
but then at the moment of "filePtr" initialization its "mode" is not
known yet, meaning that "filePtr" should be reopened again later with
the right "mode".
Putting initialization of "mode" in a list before "filePtr" doesn't
guarantied that it will be initialized before.
There is a solution to make function "setAppended" returns bool and put
it this way:
ClassA::ClassA(const std::string& fileName, bool isAppended):
filePtr(new std:fstream(fileName.c_str(),
this->setAppended(isAppended))){
}
which looks rather ugly... Putting in the same place the code of
function "setAppended" instead of calling it, duplicates the code.
So all in all I'm wondering whether "filePtr" could be initialized
somehow but in the constructor body and not its initialization list.
the case with using reference like this...
///
std:fstream& fileRef;
....
std:fstream tempRef(fileName.c_str(), mode);
fileRef = tempRef;
///
...won't work because I cannot define reference for auto_ptr like this:
const std::auto_ptr<std:fstream&> filePtr
Any tips will be appreciated!
regards,
Anton