templates

C

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;
};
 
V

Victor Bazarov

Chameleon said:
------------------------------------------------------------------------
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;
};

It doesn't make sense. Why would you want to have a template in
which one part is going to always be used if it's instantiated with
one kind of type, and the other part is always going to be used with
all other types? What's the point of having a template in that case?
I mean, how would you use it? And even if by some magic you'd make
it so you never call 'read' for 'SizeBreaker<OutputStream>', and
'write' for 'SizeBreaker<InputStream>', what is exactly the point of
having them in the same class?

V
 
A

Alf P. Steinbach

* 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
 

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

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top