1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <title>pointer_cast</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
7 <body text="#000000" bgcolor="#ffffff" link="#0000ff" vlink="#0000ff">
8 <h1><img height="86" alt="boost.png (6897 bytes)" src="../../boost.png"
9 width="277" align="middle" border="0">pointer_cast</h1>
10 <p>The pointer cast functions (<code>boost::static_pointer_cast</code> <code>boost::dynamic_pointer_cast</code>
11 <code>boost::reinterpret_pointer_cast</code> <code>boost::const_pointer_cast</code>)
12 provide a way to write generic pointer castings for raw pointers. The functions
13 are defined in <CITE><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A>.</CITE></p>
14 <P>There is test/example code in <CITE><A href="test/pointer_cast_test.cpp">pointer_cast_test.cpp</A></CITE>.</p>
15 <h2><a name="rationale">Rationale</a></h2>
16 <P>Boost smart pointers usually overload those functions to provide a mechanism to
17 emulate pointers casts. For example, <code>boost::shared_ptr<...></code> implements
18 a static pointer cast this way:</P>
20 template<class T, class U>
21 shared_ptr<T> static_pointer_cast(shared_ptr<U> const &r);
23 <P>Pointer cast functions from <CITE><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A></CITE>
24 are overloads of <code>boost::static_pointer_cast</code>, <code>boost::dynamic_pointer_cast</code>,
25 <code>boost::reinterpret_pointer_cast</code> and <code>boost::const_pointer_cast</code>
26 for raw pointers. This way when developing pointer type independent classes,
27 for example, memory managers or shared memory compatible classes, the same code
28 can be used for raw and smart pointers.</p>
29 <H2><A name="synopsis">Synopsis</A></H2>
34 template<class T, class U>
35 inline T* static_pointer_cast(U *ptr)
36 { return static_cast<T*>(ptr); }
38 template<class T, class U>
39 inline T* dynamic_pointer_cast(U *ptr)
40 { return dynamic_cast<T*>(ptr); }
42 template<class T, class U>
43 inline T* const_pointer_cast(U *ptr)
44 { return const_cast<T*>(ptr); }
46 template<class T, class U>
47 inline T* reinterpret_pointer_cast(U *ptr)
48 { return reinterpret_cast<T*>(ptr); }
53 <P>As you can see from the above synopsis, the pointer cast functions are just
54 wrappers around standard C++ cast operators.</P>
55 <H2><A name="example">Example</A></H2>
58 #include <boost/pointer_cast.hpp>
59 #include <boost/shared_ptr.hpp>
70 class derived: public base
74 template <class BasePtr>
75 void check_if_it_is_derived(const BasePtr &ptr)
77 assert(boost::dynamic_pointer_cast<derived>(ptr) != 0);
82 <I>// Create a raw and a shared_ptr</I>
84 base *ptr = new derived;
85 boost::shared_ptr<base> sptr(new derived);
87 <I>// Check that base pointer points actually to derived class</I>
89 check_if_it_is_derived(ptr);
90 check_if_it_is_derived(sptr);
98 <P>The example demonstrates how the generic pointer casts help us create pointer
102 <p>Copyright 2005 Ion GaztaƱaga. Use, modification, and distribution are subject to
103 the Boost Software License, Version 1.0. (See accompanying file <A href="../../LICENSE_1_0.txt">
104 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>