Problem with shared_ptr
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). 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); //Error when push_back. Instead of ptr, I get 0 result.push_back(ptr); } Thanks.
Are you sure ptr is non-zero? Probably, dynamic_pointer_cast failed
and returned 0.
Dynamic pointer cast works, and it points to the same address as its base class pointer, I checked. But when I push it, it disappears.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hello, An idea came to me that it is probably convevient and elegant to construct an accumulator to calculate the geometric properties, such as area, center of gravity, 1st/2nd moment, etc., of a section of, say, a ship. The section may consist of concentrated area, representing stiffener, and 'plating' with even thickness. Is there such example out of there? or, how can I build such an accumulator? I'm still very new to this lib. Thanks for any information in advance. Max B/Rgds
2009/1/14 Germán Diago <germandiago@gmail.com>
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.
Germán Diago: ...
std::vector<boost::shared_ptr<DerivedClass> > result(get_base_objects.size());
You're creating a vector full of NULLs here. :-) -- Peter Dimov http://www.pdplayer.com
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).
std::vector<boost::shared_ptr<BaseClass> > baseobjects = get_base_objects(path);
std::vector<boost::shared_ptr<DerivedClass> > result(get_base_objects.size());
Your problem is the line above. You're creating a vector of 'get_base_objects.size()' default-constructed shared pointers. Either use reserve() to grab that much space, or use assignment bellow.
BOOST_FOREACH (boost::shared_ptr<BaseClass> en, baseobjects) { boost::shared_ptr<DerivedClass> ptr = boost::dynamic_pointer_cast<DerivedClass>(en);
//Error when push_back. Instead of ptr, I get 0 result.push_back(ptr); }
-- Nikolai
participants (6)
-
Germán Diago
-
Igor R
-
Max
-
Nikolai N Fetissov
-
Peter Dimov
-
Roman Perepelitsa