How to write a virtual function in the inner class of a template class?

I have an inner class in myIteratormy template class linearLinkedList<T>, and I would like to override the inherited virtual methods from simpleIterator<T>, but the compiler rejects them, because "templates cannot be virtual." Based on this question , it seems that this should be possible, since it depends only on the type of the class. For example, the method fooin my code below is legal. How can I implement virtual functions of the inner class?

template <class T>
class linearLinkedList
{
public:
...
virtual void foo(T data); //OK
simpleIterator<T> * iterator();
private:
...
class myIterator : public simpleIterator<T>
{
  public:
    myIterator(node<T> ** head);
    virtual ~myIterator(); //inherited from simpleIterator; error when implemented
  private:
    node<T> ** prev;
    node<T> ** next;
    //functions inherited from simpleIterator<T>:
    virtual bool hasNext_impl(); //error when implemented
    virtual T next_impl();
    virtual void remove_impl();
};
...
template<class T>
virtual linearLinkedList<T>::myIterator::~myIterator() { ... }
->
linearLinkedList.h:213:1: error: templates may not be Γ’virtualΓ’
virtual linearLinkedList<T>::myIterator::~myIterator()
+4
source share
1 answer

-, , virtual, :

Β§ 7.1.2 [dcl.fct.spec]/p1:

.

function-specifier:
    inline
    virtual
    explicit

virtual - ; . 10.3.

virtual :

template<class T>
virtual linearLinkedList<T>::myIterator::~myIterator() { ... }
~~~~~~^ to be removed
+3

Source: https://habr.com/ru/post/1611550/


All Articles