Virtual methods and template classes

I have a problem, I think it is very specific.

I have 2 classes, a B aseclass and D class (from B aseclass). B is a template template (or class template) and has a clean virtual method. virtual void work(const T &dummy) = 0; It is expected that class D , which must be reimplemented, but since D is derived from B , not D , is another template class, the compiler spits on me that virtual functions and templates do not work right away.

Any ideas how to accomplish what I want?

I am grateful for any thoughts and ideas, especially if you all have already developed this problem.

this class is fixed as AS IS, I cannot edit it without violating the 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 it void B<T>::work(const T&) [with T = int*]is purely virtual within D

+5
source share
4 answers

You put const in the wrong place. Try

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

const int*coincides with int const*, i.e. matches a constant to an int, not a pointer.

+9
source

Try:

int* const& dummy
+1
source

?

g++ 4.4 :

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

int main(){return 0;}

: - D, const:

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

int main(){D d;return 0;}
0

const . :

template <typename T>
struct B {
virtual void work(T dummy) = 0;
};

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

int main() {
    D d;
    d.work( 0 );
}
0

All Articles