Protected Access Issue (with regards to extending streambuf)
I'm trying to create an ostream that will prepend a string to the
start of each line. The string will change over time (in fact, it
will be time-based). It is intended for simple logging purposes:
10:01:10 Some event
11:02:05 Another event
for example, although the strings will be more complicated in this
application.
My original design for this was to create a new streambuf class for my
purpose. It would accept a pointer to another streambuf as an
argument and forward all of the protected streambuf interface calls to
it; in xsputn, I would do the prepending before making calls to the
underlying xsputn. This scheme essentially follows the Decorator
pattern.
I have found that this design executed as above because C++ prevents
calling a protected member function on an instance of the parent in
the derived class. The following, for example, is illegal:
class P
{
protected:
virtual func();
};
class D : public P
{
private:
P* parent_;
protected:
virtual func()
{
parent_->func();
}
};
I have read in other posts the reason for this feature (this is one
http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/deed287238144810/914cfae922a2fdff?lnk=st&q=can%27t+call+protected+member+on+base+class+instance&rnum=4&hl=en#914cfae922a2fdff).
I don't think that the reason entirely justifies this limitation. One
problem that I have with it is that it makes the sematics of protected
quite different from those of private. Another problem that I have
with it is that the justification seems to be based upon protected
data members, whereas in this case I want to use a protected interface
(which should not have the same pitfalls). While using the 'friend'
keyword will eliminate this issue in many situations, it obviously
can't be applied in situations where one doesn't control the code for
the base class (std::streambuf, for example).
The only alternative that I see is to tie my streambuf class more
closely to the fstream streambuf. In particular, I can extend the
fstream streambuf and re-implement xsputn. While this will satisfy my
immediate need, I wonder if anyone has any suggestions for how to
implement this functionality so that it would work with any
std::ostream. Thank you!
Tony
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]