Boost logo

Boost Users :

Subject: Re: [Boost-users] variant: bounded types derive from same base class
From: Hicham Mouline (hicham_at_[hidden])
Date: 2009-08-04 02:21:43


From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Ovanes Markarian
Sent: 03 August 2009 18:32
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] variant: bounded types derive from same base
class

sorry, forgot to derive ABCDerived1 form ABCBase ;) But hope you got the
intention.
On Mon, Aug 3, 2009 at 5:33 PM, Ovanes Markarian <om_boost_at_[hidden]>
wrote:
Hi!

The only solution I can come up with is the declaration of the visitor in
your ABCBase as virtual function and visitor has a function template to
dispatch different types:

class ABCVisitor
{
    public:
    template<class T>
    void visit(T& t)
    {
      // no visit implemented ...
    }
};

class ABCBase
{
   //... dtor, copy ctor, assignment operator etc.

   public:
   void accept_visitor(ABCVisitor& v)
   {
       do_accept_visitor(v);
   }  

   private:
   virtual void accept_visitor(ABCVisitor& v)=0;
};

class ABCDerived1 : public ABCBase
{
   //... dtor, copy ctor, assignment operator etc.
   void accept_visitor(ABCVisitor& v)
   {
       v.visit(*this);
   }
};

template<>
void ABCVisitor::visit<ABCDerived1>(ABCDerived1& derived)
{
   // handle derived here...
}

Hope that helps,
Ovanes
----------------------------------------------------------------------------
-----------------
Unfortunately, that would require changing the ABCBase to add those 2
member functions.
I can't touch ABCBase nor

Really, I have an existing hierarchy of classes which I cannot touch, but I
still would like to have
"external" polymorphism behaviour without the need to add base class virtual
functions.

Rds,


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net