Hello everyone,
I've been trying to work with the visitor design pattern, and it works
fine except for the following.
Let's suppose that we have a fixed hierarchy of classes (many of them)
which I cannot modify. I decided to use the visitor design pattern
depending on the actual type of the classes because those classes
already support the loki visitor.
#include <Loki/Visitor.h>
// forward declarations
class Class_base;
class Class1;
class Class2;
....
class Class20;
// Class list, all derive from Class_base and they all accept the
visitor design pattern through
// the loki visitor
typedef LOKI_TYPELIST_2 0(Class1, Class2,...,Clas s20) ClassList;
//! Class visitor
typedef Loki::CyclicVis itor<void, ClassList ClassVisitor;
class VisitorImpl : public ClassVisitor {
// treat visitor as a functor
void operator(Class_ base& b) { b.Accept(*this) ; }
ReturnType Visit(Class1& c1) { // do stuff with Class1; }
ReturnType Visit(Class2& c2) { // do stuff with Class2; }
...
ReturnType Visit(Class20& c20) { // do stuff with Class20; }
};
Well, this works well, but it seems very silly to add 20 different
lines if each of those functions call to a templated function in their
implementation called Compute (that's why this function is not
virtual). So I decided to give a try to the hierarchy generator (also
provided by loki) as follows:
template <class Class>
class ClassComputerUn it {
public:
virtual typename ClassVisitor::R eturnType Visit(Class& el) {
// call Compute function
}
virtual ~ClassComputerU nit() {}
};
class VisitorImpl : public ClassVisitor, public
GenScatterHiera rchy<ClassList, ClassComputerUn it {
// ... unrelated stuff
// create visitor as a functor
void operator(Class_ base& b) { b.Accept(*this) ; }
// all 20 Visit functions don't show up anymore, they are supposed
to be inherited through the
// GenScatterHiera rchy generator
};
Now, what GenScatterHiera rchy does, is to derive from
ClassComputerUn it<Classwhere Class is each of those classes in the
ClassList. So now I'm supposed to have all 20 Visit functions in the
VisitorImpl class overide the one of the Visitor. However, this is not
the case and I cannot compile my code because there are pure virtual
functions. So in a sense the GenScatterHiera rchy did not overide the
Visit function from the visitor as I thought it would.
Does anyone have a clue why is this happening? I'm using GCC4.3.
Thanks in advance for the time reading such a long post.
aa
I've been trying to work with the visitor design pattern, and it works
fine except for the following.
Let's suppose that we have a fixed hierarchy of classes (many of them)
which I cannot modify. I decided to use the visitor design pattern
depending on the actual type of the classes because those classes
already support the loki visitor.
#include <Loki/Visitor.h>
// forward declarations
class Class_base;
class Class1;
class Class2;
....
class Class20;
// Class list, all derive from Class_base and they all accept the
visitor design pattern through
// the loki visitor
typedef LOKI_TYPELIST_2 0(Class1, Class2,...,Clas s20) ClassList;
//! Class visitor
typedef Loki::CyclicVis itor<void, ClassList ClassVisitor;
class VisitorImpl : public ClassVisitor {
// treat visitor as a functor
void operator(Class_ base& b) { b.Accept(*this) ; }
ReturnType Visit(Class1& c1) { // do stuff with Class1; }
ReturnType Visit(Class2& c2) { // do stuff with Class2; }
...
ReturnType Visit(Class20& c20) { // do stuff with Class20; }
};
Well, this works well, but it seems very silly to add 20 different
lines if each of those functions call to a templated function in their
implementation called Compute (that's why this function is not
virtual). So I decided to give a try to the hierarchy generator (also
provided by loki) as follows:
template <class Class>
class ClassComputerUn it {
public:
virtual typename ClassVisitor::R eturnType Visit(Class& el) {
// call Compute function
}
virtual ~ClassComputerU nit() {}
};
class VisitorImpl : public ClassVisitor, public
GenScatterHiera rchy<ClassList, ClassComputerUn it {
// ... unrelated stuff
// create visitor as a functor
void operator(Class_ base& b) { b.Accept(*this) ; }
// all 20 Visit functions don't show up anymore, they are supposed
to be inherited through the
// GenScatterHiera rchy generator
};
Now, what GenScatterHiera rchy does, is to derive from
ClassComputerUn it<Classwhere Class is each of those classes in the
ClassList. So now I'm supposed to have all 20 Visit functions in the
VisitorImpl class overide the one of the Visitor. However, this is not
the case and I cannot compile my code because there are pure virtual
functions. So in a sense the GenScatterHiera rchy did not overide the
Visit function from the visitor as I thought it would.
Does anyone have a clue why is this happening? I'm using GCC4.3.
Thanks in advance for the time reading such a long post.
aa
Comment