i need advice

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 :)
 
V

Victor Bazarov

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).

It may be nice, but it's definitely not so good. You try to initialise
the member '_out' before 'm_out', but in fact, they are initialised in
the order they are declared in the class, IOW 'm_out' is initialised
first, then '_out'. What happens in that case is that 'm_out' is getting
its value from an uninitialised pointer. What you need is to swap the
declaration statements for 'm_out' and '_out' in your class definition.
i can make a objects of this class like:
ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass();

No, that would be a function declaration.
or:
myClass mclass("test");
and all will be good

No, it won't 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?

No, I am can't help you. References, once initialised, cannot be
changed to refer to something else. You need to drop the 'm_out' and
always use (*_out) in your code.

V
 
V

vovata

Hi Victor,
What you thing about this modifications:

class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
_out(new ofstream(filename.c_str())),
m_out(*_out) { }
~myClass() { }

write(cons string &text) { m_out << text; }
void changeFile(string &newfilename)
{
this->~myClass();
new (this) myClass(newfilename);
}

protected:
auto_ptr<ostream> _out;
ostream &m_out;
}

and again:

ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass;
or:
myClass mclass("test");



Regards,
Vladimir
 
V

vovata

Hi Victor,
What you thing about this modifications:

class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :
_out(new ofstream(filename.c_str())),
m_out(*_out) { }
~myClass() { }

write(cons string &text) { m_out << text; }
void changeFile(string &newfilename)
{
this->~myClass();
new (this) myClass(newfilename);
}

protected:
auto_ptr<ostream> _out;
ostream &m_out;
}

and again:

ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass;
or:
myClass mclass("test");

Regards,
Vladimir
 
V

Victor Bazarov

vovata said:
What you thing about this modifications:

class myClass
{
public:
myClass(ostream &out=cout) : m_out(out) { }
myClass(string &filename) :

You should pass the string by a ref to const:

myClass(string const &filename) :
_out(new ofstream(filename.c_str())),
m_out(*_out) { }
~myClass() { }

write(cons string &text) { m_out << text; }

'write' has no return value type. I recommend

ostream& write(const string &text) { return m_out << text; }
void changeFile(string &newfilename)

Again, by a ref to _const_.
{
this->~myClass();
new (this) myClass(newfilename);

Well, legally this trick is allowed. And it's known to work.
Personally I don't like it.
}

protected:
auto_ptr<ostream> _out;
ostream &m_out;

The reordering of _out and m_out in the class definition is certainly
what you ought to do.
;

It looks OK, and is OK if it works for you. I just cringe every time
I see somebody uses that 'call the destructor and re-construct in place'
trick. Ah, don't mind me...
and again:

ofstream ofs("test");
myClass mclass(ofs);
or:
myClass mclass;
or:
myClass mclass("test");

It will be OK as soon as you change the 'string&' to 'string const&'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,296
Messages
2,571,535
Members
48,281
Latest member
DaneLxa72

Latest Threads

Top