[scoped_ptr] How to cast from bast type to decived type

View: New views
2 Messages — Rating Filter:   Alert me  

[scoped_ptr] How to cast from bast type to decived type

by Mario Klebsch-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello!

I have a scoped_ptr<base_t>, which I reset() to new derived_t().:


     class base_t
     {
     };

     class derived_t: public base_t
     {
         void Method(void) {}
     };

     boost::scoped_ptr<base_t> Ptr;
     Ptr.reset(new derived_t());

Prior to using a scoped_ptr, I could write:

     ((derived_t*)Ptr)->Method();


I tried the following:

     boost::dynamic_pointer_cast< derived_t, base_t >(Ptr)>Method();

but it does not compile. :-(

boost::dynamic_pointer_cast seems to only work for shared_ptr.

Can anybody help me?

73, Mario

P.S: I am using boost 1.38
- --
Mario Klebsch                            mkl@...
Innovative Dynamics GmbH                 http://www.innodyne.de
Wiesenweg 9                              Tel: +49 5304 91 19 410
38533 Vordorf / Rethen                   Fax: +49 5304 91 19 412

Geschäftsführer: Dipl. Phys. Gerhard Heidkamp
Sitz: 38533 Vordorf - Rethen, Wiesenweg 9
Handelsregister beim Amtsgericht Hildesheim HRB 100553, USt-Id Nr. DE
215 876 442

SICHERHEITSHINWEIS:
===================

Diese Email ist elektronisch signiert. InnoDyne versendet grundsätzlich
seit dem 11.9.2006 verbindliche Dokumente nur als signierte PDF -  
Dateien
oder elektronisch nachprüfbar signierte Emails. Juristisch und technisch
verbindliche Dokumente dürfen das Haus InnoDyne nicht als andere Datei-
formate verlassen. Sollten Sie ein signiertes, technisch oder kauf-
männisch/juristisch verbindliches  Dokument in einem anderen Dateiformat
als PDF oder signierte Email von InnoDyne erhalten, so ist dies sehr
wahrscheinlich ein Versehen des Versenders, eine Software - Fehlfunktion
oder schlimmer noch, eine Fälschung. Wir bitten in einem solchen Fall um
sofortige Information und Übermittlung solcher Dokumente an gl@...

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkrywpMACgkQDwNq2neCwq7SbwCfewiXKOvyymLeWOMjkLqmmPbi
5aYAnR5FTZiuHfbQHSg21QEXOZN8j3Zc
=Kz/D
-----END PGP SIGNATURE-----
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: [scoped_ptr] How to cast from bast type to decived type

by Roman Perepelitsa-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/11/5 Mario Klebsch <mkl@...>
   boost::scoped_ptr<base_t> Ptr;
   Ptr.reset(new derived_t());

Prior to using a scoped_ptr, I could write:

   ((derived_t*)Ptr)->Method();

  ((derived_t*)Ptr->get())->Method();

or a safer version:
  static_cast<derived_t*>(Ptr->get())->Method();

Roman Perepelitsa.

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users