V
vova
i have the next source code:
class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
_out(new ofstream(filename.c_str())),
m_out(*_out) { }
write(cons string &text) { m_out << text; }
protected:
ostream &m_out;
auto_ptr<ostream> _out;
}
this is good (and nice). i can make a objects of this class like:
ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass();
or:
myClass mclass("test");
and all will be good while i do not decide to make new member function
like this changeFile(string &newfilename), then i can write the next
code:
void changeFile(string &newfilename)
{
_out = auto_ptr<ofstream>(new ofstream(newfilename));
// now _out is ok
m_out = *_out;
// this is not valid; the reference to object can't be changed
// in write function i will be need make checks and run *_out << text
// i don't like this variant
}
are you can help me?
10x in advance
class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
_out(new ofstream(filename.c_str())),
m_out(*_out) { }
write(cons string &text) { m_out << text; }
protected:
ostream &m_out;
auto_ptr<ostream> _out;
}
this is good (and nice). i can make a objects of this class like:
ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass();
or:
myClass mclass("test");
and all will be good while i do not decide to make new member function
like this changeFile(string &newfilename), then i can write the next
code:
void changeFile(string &newfilename)
{
_out = auto_ptr<ofstream>(new ofstream(newfilename));
// now _out is ok
m_out = *_out;
// this is not valid; the reference to object can't be changed
// in write function i will be need make checks and run *_out << text
// i don't like this variant
}
are you can help me?
10x in advance