Search code examples
c++templatesvirtual

virtual methods and template classes


I got over a problem, I think a very specific one.

I've got 2 classes, a B aseclass and a D erived class (from B aseclass). B is a template class ( or class template) and has a pure virtual method virtual void work(const T &dummy) = 0; The D erived class is supposed to reimplement this, but as D is Derived from B rather than D being another template class, the compiler spits at me that virtual functions and templates don't work at once.

Any ideas how to accomplish what I want?

I am thankfull for any thoughts and Ideas, especially if you allready worked out that problem

this class is fixed aka AS IS, I can not edit this without breaking existing code base

template <typename T>
class B {
public:
...
virtual void work(const T &dummy) = 0;
..
};

take int* as an example

class D : public B<int*>{
...
virtual void work(const int* &dummy){ /* put work code here */ }
..
};

Edit: The compiler tells me, that void B<T>::work(const T&) [with T = int*] is pure virtual within D


Solution

  • You placed the const in the wrong place. Try

    virtual void work(int* const &dummy){ /* put work code here */ }
    

    const int* is the same as int const*, i.e. it associates the const with the int and not the pointer.