Boost logo

Boost Users :

Subject: [Boost-users] variant: bounded types derive from same base class
From: Hicham Mouline (hicham_at_[hidden])
Date: 2009-08-03 08:35:01


Hello,

I have a library with an abstract base class ABC and derived classes D1...
Dn.
These classes cannot be changed.

User code wants to write code that depends on which concrete type the object
being passed.

User writes:

void f( const ABC& abc )
{
   Tag t = abc.GetTag() ; // Tag is an enum for all the derived types,
delivered by the lib
    Switch (t)
    { case:
       case:
       case:
   }
}
/// SO ugly, nevertheless if some case is missing, g++ for e.g. prints
warnings

I am told this is the visitor pattern

I then looked at boost::variant<>

The lib could deliver

typedef boost::variant<D1, ... , Dn> ABCDerived;

and the user could write visitors to visit ABCDerived objects,
here the user is enforced to implement all cases in the visitor.

but then the function f expects ABC objects. Can I still do:

void f( const ABC& abc )
{
   // how to write code to use the visitors with abc ?
}

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