Hello all,
Well, I think I need to elaborate upon my previous post. My simplified
version of things did not provide enough context...
I am maintaining a very poor existing code base (isn't it the bane of all
our existences???). There is a class which contains as a data member an
ofstream object which is used for logging. This object is bound to a log
file upon object construction, so the stream's ready to write to by the time
we start doing any real work. So far so good... Where it gets messy is
that there is also a member function SetStream() to change the stream
dynamically. So, we may log to the initial stream for awhile, and then
decide to start logging to another stream. Seems like a reasonable thing to
want to do. However, SetStream() takes an ofstream parameter by value and
then assigns it to the member ofstream object. There are two problems
here - ofstream has neither a copy constructor nor an assignment operator,
so passing it as a parameter and assigning it are both illegal! This should
have never compiled, but we had a poor compiler (VC++ 6.0) and it was let
through. Now that we've upgraded to a more compliant compiler, the error is
finally getting caught.
So, I need to figure out a way to update this stream data member
dynamically. I have considered changing the data member from ofstream to
ofstream*, but there's a problem. The initial stream (upon object
construction) would have to be dynamically allocated, which means it would
have to be deallocated in the destructor. However, the client code that
uses this class passes non-dynamically allocated ofstream objects when it
calls SetStream(). So, we can start passing pointers rather than the
objects themselves, but the pointer can't be deallocated by the
destructor... I guess I could use a flag to mark whether the pointer points
to the orignal dynamically-allocated object or not, but there's got to be a
more elegant way...
My earlier attempt (in my original post) at trying to find a way to solve
this with reference members was off in la la land because an initializer
list can't initialize a reference member if the class in question has no
copy constructor. If this makes no sense, don't worry about it; there's no
need to go back and look at the original post. It was headed in the wrong
direction...
Thanks again,
Dave