typedef inheritance from a pure abstract base

Edit: Found duplicate

I've whittled down some problem code to the simplest working case to illustrate the following: my typedef in a pure abstract base class is not being inherited by the derived class. In the code below I'd like to inherit the system_t typedef into the ConcreteTemplateMethod:

#include 

// pure abstract template-method
template    // T == Analyzer
class TemplateMethod {
  public:
    typedef T system_t;

    virtual void fn (const system_t& t) const = 0;
};


template 
class Analyzer {
  public:
    void TemplatedAlgorithm (const TemplateMethod< Analyzer  >& a) const {
      printf ("Analyzer::TemplatedAlgorithm\n");
      a.fn(*this);  // run the template-method
    }

    void fn () const {
      printf ("Analyzer::fn\n");
    }
};


// concrete template-method
template 
class ConcreteTemplateMethod : public TemplateMethod < Analyzer > {
  public:
    typedef Analyzer system_t;

    virtual void fn (const system_t& t) const {
      printf ("ConcreteTemplateMethod::fn\n");
      t.fn(); // perform Analyzer's fn
    }
};

int main () {

  Analyzer  a;
  ConcreteTemplateMethod dtm;
  a.TemplatedAlgorithm(dtm);

  return 0;
}

This code compiles and runs as expected. In the ConcreteTemplateMethod the following is required, and when removed causes compiler errors:

typedef Analyzer system_t;

Note that the system_t type is already typedef'ed in the base class, however. Why must I include another typedef when inheriting?

I realize that I can qualify the typename of system_t in the derived ConcreteTemplateMethod by using typename TemplateMethod >::system_t&, but that's a bit verbose, and I'd like to avoid having to re-typedef to the base everytime I inherit and need to use that same system_t. Is there a way around this that I can define in the base TemplateMethod?

8
задан Community 23 May 2017 в 12:30
поделиться