1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
4 <title>pointer_cast.hpp</title>
7 <h1><IMG height="86" alt="C++ Boost" src="../../boost.png" width="277" align="middle" border="0">Pointer
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>
19 template<class T, class U>
20 shared_ptr<T> static_pointer_cast(shared_ptr<U> const &r);
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>
33 template<class T, class U>
34 inline T* static_pointer_cast(U *ptr)
35 { return static_cast<T*>(ptr); }
37 template<class T, class U>
38 inline T* dynamic_pointer_cast(U *ptr)
39 { return dynamic_cast<T*>(ptr); }
41 template<class T, class U>
42 inline T* const_pointer_cast(U *ptr)
43 { return const_cast<T*>(ptr); }
45 template<class T, class U>
46 inline T* reinterpret_pointer_cast(U *ptr)
47 { return reinterpret_cast<T*>(ptr); }
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>
57 #include <boost/pointer_cast.hpp>
58 #include <boost/shared_ptr.hpp>
69 class derived: public base
73 template <class BasePtr>
74 void check_if_it_is_derived(const BasePtr &ptr)
76 assert(boost::dynamic_pointer_cast<derived>(ptr) != 0);
81 <I>// Create a raw and a shared_ptr</I>
83 base *ptr = new derived;
84 boost::shared_ptr<base> sptr(new derived);
86 <I>// Check that base pointer points actually to derived class</I>
88 check_if_it_is_derived(ptr);
89 check_if_it_is_derived(sptr);
97 <P>The example demonstrates how the generic pointer casts help us create pointer
100 <p>Revised: $Date: 2005-12-06 05:26:13 -0800 (Tue, 06 Dec 2005) $</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>