need a virtual template member workaround

Posted by yurib on Stack Overflow See other posts from Stack Overflow or by yurib
Published on 2010-05-30T18:46:43Z Indexed on 2010/05/30 18:52 UTC
Read the original article Hit count: 163

Filed under:
|
|
|

Hello,

I need to write a program implementing the visitor design pattern. The problem is that the base visitor class is a template class. This means that BaseVisited::accept() takes a template class as a parameter and since it uses 'this' and i need 'this' to point to the correct runtime instance of the object, it also needs to be virtual.
I'd like to know if there's any way around this problem.

template <typename T>
class BaseVisitor {
  public:
    BaseVisitor();
    T visit(BaseVisited *visited);
    virtual ~BaseVisitor();
}


class BaseVisited {
  BaseVisited();
  template <typename T>
    virtual void accept(BaseVisitor<T> *visitor) { visitor->visit(this); }; // problem
  virtual ~BaseVisited();
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates