static cast with boost:: shared PST?

Jaki jest odpowiednik static_cast z boost::shared_ptr?

Innymi słowy, jak mam przepisać następujące

Base* b = new Derived();
Derived* d = static_cast<Derived*>(b);

Podczas używania shared_ptr?

boost::shared_ptr<Base> b(new Derived());
boost::shared_ptr<Derived> d = ???
Author: peterchen, 2009-03-09

4 answers

Użycie boost::static_pointer_cast:

boost::shared_ptr<Base> b(new Derived());
boost::shared_ptr<Derived> d = boost::static_pointer_cast<Derived>(b);
 104
Author: Frank,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-12 09:50:17

Istnieją trzy operatory cast dla inteligentnych wskaźników: static_pointer_cast, dynamic_pointer_cast, i const_pointer_cast. Są one albo w przestrzeni nazw boost (dostarczonej przez <boost/shared_ptr.hpp>) lub przestrzeni nazw std::tr1 (dostarczonej przez Boost lub przez implementację tr1 kompilatora).

 22
Author: Michael Kristofik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-09 03:15:40

Jako komentarz: jeśli Derived faktycznie pochodzi z bazy, to powinieneś użyć dynamic_pointer_cast zamiast odlewów statycznych. System będzie miał szansę wykryć, kiedy/Czy Twój rzut nie jest poprawny.

 3
Author: David Rodríguez - dribeas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-09 06:39:58

Warto wspomnieć, że istnieje różnica w liczbie operatorów odlewania dostarczanych przez Boost i implementacji TR1.

TR1 nie definiuje trzeciego operatora const_pointer_cast ()

 2
Author: mloskot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-16 12:23:17