$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: David Klein (dave_chp_at_[hidden])
Date: 2006-09-29 11:07:05
scott velasquez wrote:
> Here is what I desire to have in my GUI library as it pertains to boost:
>
> 1.  No raw widgt pointers.  Container functions should take a reference to a 
> base class smart pointer type rather then accepting raw pointers of the base 
> class type.
> 2.  Containers store an array of smart pointers of the base GUI widget type. 
> This is needed because containers can contain many widget derived types.
>
> I'm just showing the class code that matters; Normally, you would have 
> constructors, destructors, access label, etc.
>
> class OWidget;
>
> class ODerivedWidget : public OWidget;
>
> class OWidgetContainer : public OWidget
> {
>     public:
>
>     void Add( boost::shared_ptr<OWidget>& widget )
>    {
>         mWidgets.pushback(widget);
>    }
>
>     std::vector<boost::shared_ptr<OWidget>>     mWidgets;
> }
>
> int main(void)
> {
>     OWidgetContainer root;
>
>     // Works just fine since the types match
>     boost::shared_ptr<OWidget>    base(new OWidget());
>     root.Add(base);
>
>     // Doesn't work since the Add() function can't convert ODerivedWidget
> shared pointer to OWidget shared pointer type.
>     boost::shared_ptr<ODerivedWidget>    derived(new ODerivedWidget());
>     root.Add( derived);
> }
>
> Is this even possible?  I've read about boost::any and boost:variant and 
> while that might solve my problem of storing smart pointers of differing 
> types, it doesn't allow me to pass smart pointers of derived types.
>
> Any ideas/suggestions would be GREATLY appreciated!
>
> -scottv 
>
>   
hi scott,
make your Add() function take const references and it works.
void Add( const boost::shared_ptr<OWidget>& widget )
-- HTH dave