templates and inheritance

Started by
4 comments, last by JOL 20 years, 1 month ago
I am making a small Template library for very simple filtering, I want to create classes that perform different calculations on data ''pipes''... I am trying to implement it like this. a templated base class called MyPipe that declares virtual functions...(not allowed?) a class BufferPipe that inherites MyPipe and implements all the virtual functions with arrays of the template type. a class StreamPipe that inherites MyPipe and implements the all the virtual functions with iostreams. also different templated classes, Filters, with methods for processing the data in MyPipe... the data could be a double a int or even complex, ANY type ...here''s the problem... MyPipe is supposed to work as a abstract base class and all Filters are supposed to be passed BufferPipes or StreamPipes AS if they were MyPipes. This isn''t allowed when working with templates(?)... How am I supposed to get this functionality and design without the abstract baseclass MyPipe ? Should my FilterClasses have the Pipetype as a second template parameter - this would heavily effect my design ? Is there any way that I can control the types passed as a template parameter, eg. constrain it to only be of a specific baseclass and children of that type ?
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
Advertisement
Your derived class has to inherit the base template instantiated:
class BufferPipe : public MyPipe< BufferType >{  ...}; 
An uninstantiated template does not compile into anything.
Thank's for the reply, I am doing just that though...
The problem is that the filters can't be passed a instance of a BufferPipe AS a MyPipe, it doesn't seem there can be virtual functions and RTTI and such ...

example:
template <class T>class MyPipe{  public:    virtual void func(T& t) const=0;};template <class T> class BufferPipe: public MyPipe<T>{  public:    void func(T& t){ std::cout << t;}};template <class T>void func2(MyPipe<T>& t){    T val;    t.func(val);}void main(){    BufferPipe<double> bt;    func2(bt);}   



[edited by - JOL on March 4, 2004 10:18:10 AM]
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
template <class T> class BufferPipe{  public:    void func(T& t){ std::cout << t;}};template <class Buf, class T>void func2(Buf& t){    T val;    t.func(val);}int main(int argc, char *argv[]){  BufferPipe bt;  func2<BufferPipe<double>,double>(bt);  return 0;}  


Would this be the 'right' way to go ?
How would I make sure that both The BufferPipe and the type T is both instantiated with double, (a idiotproof API-ok no such thing but) ?
should I skip inheritance altogether ?

[edited by - JOL on March 4, 2004 10:35:47 AM]
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
Correct me if I''m wrong, but it appears to me the only thing wrong with you first example is that you didn''t declare func in your derived class (BufferPipe) as const. Add the const keyword and everything compiles fine

 template  class BufferPipe: public MyPipe{  public:    void func(T& t) { std::cout << t;} // this needs to be const like in the base class}; 
Ummm....
I sort of realized my problem was not anything like this....
Thanks anyway...

Maybe this thread should be removed, might be confusing...
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work

This topic is closed to new replies.

Advertisement