| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> |
|---|
| 2 | <html> |
|---|
| 3 | <head> |
|---|
| 4 | <title>pointer_cast.hpp</title> |
|---|
| 5 | </head> |
|---|
| 6 | <body> |
|---|
| 7 | <h1><IMG height="86" alt="C++ Boost" src="../../boost.png" width="277" align="middle" border="0">Pointer |
|---|
| 8 | cast functions</h1> |
|---|
| 9 | <p>The pointer cast functions (<code>boost::static_pointer_cast</code> <code>boost::dynamic_pointer_cast</code> |
|---|
| 10 | <code>boost::reinterpret_pointer_cast</code> <code>boost::const_pointer_cast</code>) |
|---|
| 11 | provide a way to write generic pointer castings for raw pointers. The functions |
|---|
| 12 | are defined in <CITE><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A>.</CITE></p> |
|---|
| 13 | <P>There is test/example code in <CITE><A href="test/pointer_cast_test.cpp">pointer_cast_test.cpp</A></CITE>.</p> |
|---|
| 14 | <h2><a name="rationale">Rationale</a></h2> |
|---|
| 15 | <P>Boost smart pointers usually overload those functions to provide a mechanism to |
|---|
| 16 | emulate pointers casts. For example, <code>boost::shared_ptr<...></code> implements |
|---|
| 17 | a static pointer cast this way:</P> |
|---|
| 18 | <pre> |
|---|
| 19 | template<class T, class U> |
|---|
| 20 | shared_ptr<T> static_pointer_cast(shared_ptr<U> const &r); |
|---|
| 21 | </pre> |
|---|
| 22 | <P>Pointer cast functions from <CITE><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A></CITE> |
|---|
| 23 | are overloads of <code>boost::static_pointer_cast</code>, <code>boost::dynamic_pointer_cast</code>, |
|---|
| 24 | <code>boost::reinterpret_pointer_cast</code> and <code>boost::const_pointer_cast</code> |
|---|
| 25 | for raw pointers. This way when developing pointer type independent classes, |
|---|
| 26 | for example, memory managers or shared memory compatible classes, the same code |
|---|
| 27 | can be used for raw and smart pointers.</p> |
|---|
| 28 | <H2><A name="synopsis">Synopsis</A></H2> |
|---|
| 29 | <BLOCKQUOTE> |
|---|
| 30 | <PRE> |
|---|
| 31 | namespace boost { |
|---|
| 32 | |
|---|
| 33 | template<class T, class U> |
|---|
| 34 | inline T* static_pointer_cast(U *ptr) |
|---|
| 35 | { return static_cast<T*>(ptr); } |
|---|
| 36 | |
|---|
| 37 | template<class T, class U> |
|---|
| 38 | inline T* dynamic_pointer_cast(U *ptr) |
|---|
| 39 | { return dynamic_cast<T*>(ptr); } |
|---|
| 40 | |
|---|
| 41 | template<class T, class U> |
|---|
| 42 | inline T* const_pointer_cast(U *ptr) |
|---|
| 43 | { return const_cast<T*>(ptr); } |
|---|
| 44 | |
|---|
| 45 | template<class T, class U> |
|---|
| 46 | inline T* reinterpret_pointer_cast(U *ptr) |
|---|
| 47 | { return reinterpret_cast<T*>(ptr); } |
|---|
| 48 | |
|---|
| 49 | } // namespace boost |
|---|
| 50 | </PRE> |
|---|
| 51 | </BLOCKQUOTE> |
|---|
| 52 | <P>As you can see from the above synopsis, the pointer cast functions are just |
|---|
| 53 | wrappers around standard C++ cast operators.</P> |
|---|
| 54 | <H2><A name="example">Example</A></H2> |
|---|
| 55 | <BLOCKQUOTE> |
|---|
| 56 | <PRE> |
|---|
| 57 | #include <boost/pointer_cast.hpp> |
|---|
| 58 | #include <boost/shared_ptr.hpp> |
|---|
| 59 | |
|---|
| 60 | class base |
|---|
| 61 | { |
|---|
| 62 | public: |
|---|
| 63 | |
|---|
| 64 | virtual ~base() |
|---|
| 65 | { |
|---|
| 66 | } |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | class derived: public base |
|---|
| 70 | { |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | template <class BasePtr> |
|---|
| 74 | void check_if_it_is_derived(const BasePtr &ptr) |
|---|
| 75 | { |
|---|
| 76 | assert(boost::dynamic_pointer_cast<derived>(ptr) != 0); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | int main() |
|---|
| 80 | { |
|---|
| 81 | <I>// Create a raw and a shared_ptr</I> |
|---|
| 82 | |
|---|
| 83 | base *ptr = new derived; |
|---|
| 84 | boost::shared_ptr<base> sptr(new derived); |
|---|
| 85 | |
|---|
| 86 | <I>// Check that base pointer points actually to derived class</I> |
|---|
| 87 | |
|---|
| 88 | check_if_it_is_derived(ptr); |
|---|
| 89 | check_if_it_is_derived(sptr); |
|---|
| 90 | |
|---|
| 91 | // <EM>Ok!</EM> |
|---|
| 92 | |
|---|
| 93 | delete ptr; |
|---|
| 94 | return 0; |
|---|
| 95 | }</PRE> |
|---|
| 96 | </BLOCKQUOTE> |
|---|
| 97 | <P>The example demonstrates how the generic pointer casts help us create pointer |
|---|
| 98 | independent code.</P> |
|---|
| 99 | <hr> |
|---|
| 100 | <p>Revised: $Date: 2005/12/06 13:26:13 $</p> |
|---|
| 101 | <p>Copyright 2005 Ion Gaztañaga. Use, modification, and distribution are subject to |
|---|
| 102 | the Boost Software License, Version 1.0. (See accompanying file <A href="../../LICENSE_1_0.txt"> |
|---|
| 103 | LICENSE_1_0.txt</A> or a copy at <<A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>>.)</p> |
|---|
| 104 | </body> |
|---|
| 105 | </html> |
|---|