templates



 DEVELOP > c-Plus-Plus > templates

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Chameleon"
Date: 10 Jan 2008 04:24:45 PM
Object: templates
------------------------------------------------------------------------
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;
};
------------------------------------------------------------------------
I believe not, but never knows.
Thanks for your time.
.

User: "Alf P. Steinbach"

Title: Re: templates 10 Jan 2008 04:57:25 PM
* 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?
.

User: "Victor Bazarov"

Title: Re: templates 10 Jan 2008 04:32:40 PM
Chameleon wrote:

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

I believe not, but never knows.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER