File: pointer_cast.html

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (105 lines) | stat: -rw-r--r-- 3,666 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
	<head>
		<title>pointer_cast.hpp</title>
	</head>
	<body>
		<h1><IMG height="86" alt="C++ Boost" src="../../boost.png" width="277" align="middle" border="0">Pointer 
			cast functions</h1>
		<p>The pointer cast functions (<code>boost::static_pointer_cast</code> <code>boost::dynamic_pointer_cast</code>
			<code>boost::reinterpret_pointer_cast</code> <code>boost::const_pointer_cast</code>) 
			provide a way to write generic pointer castings for raw pointers. The functions 
			are defined in <CITE><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A>.</CITE></p>
		<P>There is test/example code in <CITE><A href="test/pointer_cast_test.cpp">pointer_cast_test.cpp</A></CITE>.</p>
			<h2><a name="rationale">Rationale</a></h2>
		<P>Boost smart pointers usually overload those functions to provide a mechanism to 
			emulate pointers casts. For example, <code>boost::shared_ptr&lt;...&gt;</code> implements 
			a static pointer cast this way:</P>
		<pre>
template&lt;class T, class U&gt;
    shared_ptr&lt;T&gt; static_pointer_cast(shared_ptr&lt;U&gt; const &amp;r);
</pre>
		<P>Pointer cast functions from <CITE><A href="../../boost/pointer_cast.hpp">boost/pointer_cast.hpp</A></CITE>
			are overloads of <code>boost::static_pointer_cast</code>, <code>boost::dynamic_pointer_cast</code>,
			<code>boost::reinterpret_pointer_cast</code> and <code>boost::const_pointer_cast</code>
			for raw pointers. This way when developing pointer type independent classes, 
			for example, memory managers or shared memory compatible classes, the same code 
			can be used for raw and smart pointers.</p>
			<H2><A name="synopsis">Synopsis</A></H2>
			<BLOCKQUOTE>
				<PRE>
namespace boost {

template&lt;class T, class U&gt;
inline T* static_pointer_cast(U *ptr)
  { return static_cast&lt;T*&gt;(ptr); }

template&lt;class T, class U&gt;
inline T* dynamic_pointer_cast(U *ptr)
  { return dynamic_cast&lt;T*&gt;(ptr); }

template&lt;class T, class U&gt;
inline T* const_pointer_cast(U *ptr)
  { return const_cast&lt;T*&gt;(ptr); }

template&lt;class T, class U&gt;
inline T* reinterpret_pointer_cast(U *ptr)
  { return reinterpret_cast&lt;T*&gt;(ptr); }
  
} // namespace boost
</PRE>
			</BLOCKQUOTE>
		<P>As you can see from the above synopsis, the pointer cast functions are just 
			wrappers around standard C++ cast operators.</P>
		<H2><A name="example">Example</A></H2>
		<BLOCKQUOTE>
			<PRE>
#include &lt;boost/pointer_cast.hpp&gt;
#include &lt;boost/shared_ptr.hpp&gt;

class base
{
public:

   virtual ~base()
   {
   }
};

class derived: public base
{
};

template &lt;class BasePtr&gt;
void check_if_it_is_derived(const BasePtr &amp;ptr)
{
   assert(boost::dynamic_pointer_cast&lt;derived&gt;(ptr) != 0);
}

int main()
{
   <I>// Create a raw and a shared_ptr</I>

   base *ptr = new derived;
   boost::shared_ptr&lt;base&gt; sptr(new derived);
   
   <I>// Check that base pointer points actually to derived class</I>

   check_if_it_is_derived(ptr);
   check_if_it_is_derived(sptr);
   
   // <EM>Ok!</EM>
   
   delete ptr;
   return 0;
}</PRE>
		</BLOCKQUOTE>
		<P>The example demonstrates how the generic pointer casts help us create pointer 
			independent code.</P>
		<hr>
		<p>Revised: $Date: 2005-12-06 08:26:13 -0500 (Tue, 06 Dec 2005) $</p>
		<p>Copyright 2005 Ion Gaztaaga. Use, modification, and distribution are subject to 
			the Boost Software License, Version 1.0. (See accompanying file <A href="../../LICENSE_1_0.txt">
				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>
	</body>
</html>