Re: templates
* Chameleon:
------------------------------------------------------------------------
class SizeBreaker1 : public InputStream
{
public:
void attach(InputStream &stream) { strm = &stream; }
virtual int read(void *base, int size);
private:
InputStream *strm;
};
class SizeBreaker2 : public OutputStream
{
public:
void attach(OutputStream &stream) { strm = &stream; }
virtual int write(const void *base, int size, bool flush = false);
private:
OutputStream *strm;
};
------------------------------------------------------------------------
I am not ...guru on templates, so I have this question:
Can I merge 2 classes above in a templated class?
Something like that:
------------------------------------------------------------------------
template<class T>
class SizeBreaker<T> : public T
{
public:
void attach(T &stream) { strm = &stream; }
// You must help here!
//virtual int read(void *base, int size);
//virtual int write(const void *base, int size, bool flush = false);
private:
T *strm;
};
------------------------------------------------------------------------
As Victor noted else-thread, it really doesn't make much sense.
But that may be because we're missing the larger context of what you're
doing.
Anyways, if the question is how to reduce redundancy you could do
template< class StreamType >
class AttachableStream: public StreamType
{
public:
AttachableStream(): myStream( 0 ) {}
void attach( T& aStream ) { myStream = &aStream; }
bool isAttached() const { return (myStream != 0); }
private:
T* myStream;
};
class AttachableOutputStream: public AttachableStream<OutputStream>
{
public:
virtual int write ...
};
And ditto for input.
By the way, are you sure you want those void* pointers there? That's
where templating might help you.
Also, names like SizeBreaker don't communicate the intent well, so,
suggest renaming like above, or whatever the intent is.
Finally, consider, if possible, removing the "attach" function and doing
that in the constructor, and also consider whether the class derivation
really makes sense (are you inheriting from interface classes?).
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?