Boost logo

Boost Users :

Subject: Re: [Boost-users] Problem with shared_ptr
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2009-01-14 08:04:13


2009/1/14 Germán Diago <germandiago_at_[hidden]>

> What's wrong with this code? When I push_back ptr to the vector, it pushes
> 0 instead of
> the value contained in ptr (which I checked and is non-zero).
>
>
Try this:

std::vector<boost::shared_ptr<BaseClass> > baseobjects =
get_base_objects(path);

    std::vector<boost::shared_ptr<DerivedClass> >
result(get_base_objects.size());
    BOOST_FOREACH (boost::shared_ptr<BaseClass> en, baseobjects)
    {
        boost::shared_ptr<DerivedClass> ptr =
boost::dynamic_pointer_cast<DerivedClass>(en);
        if (!ptr)
            std::cout << "Dynamic cast failed!" << std::endl;
        result.push_back(ptr);
        if (!result.back())
            std::cout << "Push back failed!" << std::endl;
    }

And check what it prints.

Roman Perepelitsa.



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