Overriding a pure virtual function from template classes

I was looking for a stack overflow, but could not find what exactly answers my question. I have an interface class that contains only pure virtual functions that I would like to implement with classes that come from this class.

I have an interface, which I will call BaseInterface , which defines functions that I would like to override in all classes that come from this interface. In this example, let's say there is only one pure virtual function called toImplement. I am creating a class called Base that inherits from BaseInterface and adds some functions to the inherited pure virtual functions. Base is still an abstract class because it does not implement functions in BaseInterface .

I have several classes that are derived from Base that benefit from the overall functionality of Base , but indicate what happens when toImplement runs on its instances. These classes must be specific and satisfy all the requirements set by BaseInterface . Below I define one of these classes called Derived .

All this works fine when BaseInterface and Base are not templates. The code compiles and works fine, without defining (1.) or implementing (2.) toImplement in Base .

However, I would like to work with different types. From what I understand, it's nice to have pure virtual functions in a template class. I have a BaseInterface and Base template for some type T. When I do not define toImplement in Base (1.), I cannot compile, because Base does not know which toImplement to use in tryUsingImplemented. If you now add the definition to the Base , the code is precompiled, but the linker cannot find the Base :: toImplement implementation. Finally, if I define and implement toImplement in Base (1. and 2.), the code compiles.

, toImplement Base, , . , toImplement, Derived . BaseInterface .

- , toImplement Derived, Base, ?

template <typename T>
class BaseInterface {
   virtual void toImplement(T & t) = 0;
};


template <typename T>
class Base : public BaseInterface<T> {
bool m_useImplemented;

public:
   explicit Base(bool useImplemented) : m_usedImplemented(useImplemented) {}

   void tryUsingImplemented(T & t) {
      if (m_useImplemented)
         toImplement(t);
   }  

protected:
   // 1: Defining toImplement pure virtual function in Base
   virtual void toImplement(T & t);
};

// 2. Implementing a version of toImplement in Base which does nothing
template <typename T>
inline void Base<T>::toImplement(T & t) {
   // do nothing
}

class Derived : public Base<int> {
public:
   explicit Derived(bool useImplemented) : Base<int>(useImplemented) {}

protected:
   // 3. implementing toImplement in Derived 
   void toImplement(T & t) {
      std::cout << "Doing stuff for Derived" << std::endl;
   }  

};
+3
2

, , .

. :

  • toImplement BaseInterface.
  • tryUsingImplemented . . , this->toImplement(t).
+2

toImplement protected BaseInterface, . Derived::toImplement int, . , Derived , , Base.

  #include <iostream>

  template <typename T>
  class BaseInterface {
  protected:
     virtual void toImplement(T & t) = 0;
  };


  template <typename T>
  class Base : public BaseInterface<T> {
  bool m_useImplemented;

  public:
     explicit Base(bool useImplemented) : m_useImplemented(useImplemented) {}

     void tryUsingImplemented(T & t) {
        if (m_useImplemented)
           toImplement(t);
     }  
  };

  class Derived : public Base<int> {
  public:
     explicit Derived(bool useImplemented) : Base<int>(useImplemented) {}

  protected:
     // 3. implementing toImplement in Derived 
     void toImplement(int & t) {
        std::cout << "Doing stuff for Derived" << std::endl;
     }  

  };
0

All Articles