Merlin wrote:
[color=blue]
> Hi Murali
> How would u go about using templates in such a circumstance. Can u give
> an example. Make one up if mine is not any good for ur purposes. I am
> curious in what you have said.
>
> Thanks[/color]
Hi Merlin,
I dont have GOF. B'coz of that I could not Understand your Question.
visit this page:
http://home.earthlink.net/~huston2/dp/VisitorDemosCpp
an example starts with "// Purpose. Visitor design pattern"
Visitor class is like this..
class Visitor { public:
virtual void visit( This* e ) = 0;
virtual void visit( That* e ) = 0;
virtual void visit( TheOther* e ) = 0;
};
One of Visitor Child class is like this..
class UpVisitor : public Visitor {
/*virtual*/ void visit( This* e ) {
cout << "do Up on " + e->thiss() << '\n'; }
/*virtual*/ void visit( That* e ) {
cout << "do Up on " + e->that() << '\n'; }
/*virtual*/ void visit( TheOther* e ) {
cout << "do Up on " + e->theOther() << '\n'; }
};
and some other classes...
// 1. Add an accept(Visitor) method to the "element" hierarchy
class Element { public:
virtual void accept( class Visitor& v ) = 0;
};
class This : public Element { public:
/*virtual*/ void accept( Visitor& v );
string thiss() { return "This"; }
};
Similarly That class, TheOther classes are defined in the same way.
accept method is overridden so we cannot use templates on that.
/*virtual*/ void This::accept( Visitor& v ) { v.visit( this ); }
/*virtual*/ void That::accept( Visitor& v ) { v.visit( this ); }
/*virtual*/ void TheOther::accept( Visitor& v ) { v.visit( this ); }
Class hierarchy is this..
Element <- This, That, TheOther
Visitor <- UpVisitor, DownVisitor
but.. visit method is overloaded in UpVisitor & DownVisitor. we can use
templates on that.
Instead of e->thiss(), e->That(), e->TheOTher() calls used in Visit
method, if we create a common method in all classes (This, That &
TheOther), say getName() we can use templates. Other wise there is no
other way. If the Visit method has to behave differently for different
parameters, you have to overload one by one.
Plz let me know if this is near to your Question.
Wish You all Happy New Year,
Murali Krishna.