Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/smart_ptr/pointer_cast.html @ 45

Last change on this file since 45 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.6 KB
Line 
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&lt;...&gt;</code> implements
17                        a static pointer cast this way:</P>
18                <pre>
19template&lt;class T, class U&gt;
20    shared_ptr&lt;T&gt; static_pointer_cast(shared_ptr&lt;U&gt; const &amp;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>
31namespace boost {
32
33template&lt;class T, class U&gt;
34inline T* static_pointer_cast(U *ptr)
35  { return static_cast&lt;T*&gt;(ptr); }
36
37template&lt;class T, class U&gt;
38inline T* dynamic_pointer_cast(U *ptr)
39  { return dynamic_cast&lt;T*&gt;(ptr); }
40
41template&lt;class T, class U&gt;
42inline T* const_pointer_cast(U *ptr)
43  { return const_cast&lt;T*&gt;(ptr); }
44
45template&lt;class T, class U&gt;
46inline T* reinterpret_pointer_cast(U *ptr)
47  { return reinterpret_cast&lt;T*&gt;(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 &lt;boost/pointer_cast.hpp&gt;
58#include &lt;boost/shared_ptr.hpp&gt;
59
60class base
61{
62public:
63
64   virtual ~base()
65   {
66   }
67};
68
69class derived: public base
70{
71};
72
73template &lt;class BasePtr&gt;
74void check_if_it_is_derived(const BasePtr &amp;ptr)
75{
76   assert(boost::dynamic_pointer_cast&lt;derived&gt;(ptr) != 0);
77}
78
79int main()
80{
81   <I>// Create a raw and a shared_ptr</I>
82
83   base *ptr = new derived;
84   boost::shared_ptr&lt;base&gt; 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 &lt;<A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>&gt;.)</p>
104        </body>
105</html>
Note: See TracBrowser for help on using the repository browser.