1 <?xml version=
"1.0" encoding=
"utf-8" ?>
2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8" />
6 <meta name=
"generator" content=
"Docutils 0.3.10: http://docutils.sourceforge.net/" />
7 <title>Boost Pointer Container Library
</title>
8 <style type=
"text/css">
11 :Author: David Goodger
12 :Contact: goodger@users.sourceforge.net
15 :Copyright: This stylesheet has been placed in the public domain.
17 Default cascading style sheet for the HTML output of Docutils.
19 See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
20 customize this style sheet.
23 /*
"! important" is used here to override other ``margin-top`` and
24 ``margin-bottom`` styles that are later in the stylesheet or
25 more specific. See http://www.w3.org/TR/CSS1#the-cascade */
27 margin-top:
0 ! important }
29 .last, .with-subtitle {
30 margin-bottom:
0 ! important }
36 text-decoration: none ;
43 margin-bottom:
0.5em }
45 /* Uncomment (and remove this text!) to get bold-faced definition list terms
53 div.abstract p.topic-title {
57 div.admonition, div.attention, div.caution, div.danger, div.error,
58 div.hint, div.important, div.note, div.tip, div.warning {
60 border: medium outset ;
63 div.admonition p.admonition-title, div.hint p.admonition-title,
64 div.important p.admonition-title, div.note p.admonition-title,
65 div.tip p.admonition-title {
67 font-family: sans-serif }
69 div.attention p.admonition-title, div.caution p.admonition-title,
70 div.danger p.admonition-title, div.error p.admonition-title,
71 div.warning p.admonition-title {
74 font-family: sans-serif }
76 /* Uncomment (and remove this text!) to get reduced vertical space in
78 div.compound .compound-first, div.compound .compound-middle {
79 margin-bottom:
0.5em }
81 div.compound .compound-last, div.compound .compound-middle {
90 div.dedication p.topic-title {
97 div.footer, div.header {
106 div.line-block div.line-block {
113 border: medium outset ;
115 background-color: #ffffee ;
120 div.sidebar p.rubric {
121 font-family: sans-serif ;
124 div.system-messages {
127 div.system-messages h1 {
131 border: medium outset ;
134 div.system-message p.system-message-title {
141 h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
142 h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
163 ol.simple, ul.simple {
167 list-style: decimal }
170 list-style: lower-alpha }
173 list-style: upper-alpha }
176 list-style: lower-roman }
179 list-style: upper-roman }
193 white-space: nowrap }
202 font-family: sans-serif ;
207 font-family: sans-serif ;
223 pre.literal-block, pre.doctest-block {
226 background-color: #eeeeee }
229 font-family: sans-serif ;
230 font-style: oblique }
232 span.classifier-delimiter {
233 font-family: sans-serif ;
237 font-family: sans-serif }
240 white-space: nowrap }
248 span.section-subtitle {
249 /* font-size relative to parent (h1..h6 element) */
253 border-left: solid thin gray }
260 margin-bottom:
0.5em }
263 border-left: solid thin black }
265 table.docutils td, table.docutils th,
266 table.docinfo td, table.docinfo th {
267 padding-left:
0.5em ;
268 padding-right:
0.5em ;
269 vertical-align: top }
271 table.docutils th.field-name, table.docinfo th.docinfo-name {
274 white-space: nowrap ;
277 h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
278 h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
282 background-color: #eeeeee }
285 list-style-type: none }
290 <div class=
"document" id=
"boost-pointer-container-library">
291 <h1 class=
"title"><img alt=
"Boost" src=
"boost.png" /> Pointer Container Library
</h1>
292 <h2 class=
"subtitle" id=
"usage-guidelines">Usage Guidelines
</h2>
293 <div class=
"contents local topic">
295 <li><a class=
"reference" href=
"#choosing-the-right-container" id=
"id8" name=
"id8">Choosing the right container
</a></li>
296 <li><a class=
"reference" href=
"#recommended-practice-for-object-oriented-programming" id=
"id9" name=
"id9">Recommended practice for Object-Oriented Programming
</a><ul>
297 <li><a class=
"reference" href=
"#make-base-classes-abstract-and-without-data" id=
"id10" name=
"id10">1. Make base classes abstract and without data
</a></li>
298 <li><a class=
"reference" href=
"#make-virtual-functions-private-and-provide-a-non-virtual-public-forwarding-function" id=
"id11" name=
"id11">2. Make virtual functions private and provide a non-virtual public forwarding function
</a></li>
299 <li><a class=
"reference" href=
"#derive-your-base-class-from-boost-noncopyable" id=
"id12" name=
"id12">3. Derive your base class from
<tt class=
"docutils literal"><span class=
"pre">boost::noncopyable
</span></tt></a></li>
300 <li><a class=
"reference" href=
"#avoid-null-pointers-in-containers-if-possible" id=
"id13" name=
"id13">4. Avoid null-pointers in containers (if possible)
</a></li>
305 <div class=
"section">
306 <h1><a class=
"toc-backref" href=
"#id8" id=
"choosing-the-right-container" name=
"choosing-the-right-container">Choosing the right container
</a></h1>
307 <p>The recommended usage pattern of the container classes is the same as
308 for normal standard containers.
</p>
309 <p><tt class=
"docutils literal"><span class=
"pre">ptr_vector
</span></tt>,
<tt class=
"docutils literal"><span class=
"pre">ptr_list
</span></tt> and
<tt class=
"docutils literal"><span class=
"pre">ptr_deque
</span></tt> offer the programmer different
310 complexity tradeoffs and should be used accordingly.
<tt class=
"docutils literal"><span class=
"pre">ptr_vector
</span></tt> is the
311 type of sequence that should be used by default.
<tt class=
"docutils literal"><span class=
"pre">ptr_list
</span></tt> should be used
312 when there are frequent insertions and deletions from the middle of the
313 sequence and if the container is fairly large (eg. more than
100
314 elements).
<tt class=
"docutils literal"><span class=
"pre">ptr_deque
</span></tt> is the data structure of choice when most insertions
315 and deletions take place at the beginning or at the end of the sequence.
316 The special container
<tt class=
"docutils literal"><span class=
"pre">ptr_array
</span></tt> may be used when the size of the container is invariant
317 and known at compile time.
</p>
318 <p>An associative container supports unique keys if it may contain at most
319 one element for each key. Otherwise, it supports equivalent keys.
320 <tt class=
"docutils literal"><span class=
"pre">ptr_set
</span></tt> and
<tt class=
"docutils literal"><span class=
"pre">ptr_map
</span></tt> support unique keys.
321 <tt class=
"docutils literal"><span class=
"pre">ptr_multiset
</span></tt> and
<tt class=
"docutils literal"><span class=
"pre">ptr_multimap
</span></tt>
322 support equivalent keys.
</p>
324 <div class=
"section">
325 <h1><a class=
"toc-backref" href=
"#id9" id=
"recommended-practice-for-object-oriented-programming" name=
"recommended-practice-for-object-oriented-programming">Recommended practice for Object-Oriented Programming
</a></h1>
326 <p>Idiomatic Object-Oriented Programming in C++ looks a bit different from
327 the way it is done in other languages. This is partly because C++
328 has both value and reference semantics, and partly because C++ is more flexible
329 than other languages. Below is a list of recommendations that you are
330 encouraged to follow:
</p>
331 <div class=
"section">
332 <h2><a class=
"toc-backref" href=
"#id10" id=
"make-base-classes-abstract-and-without-data" name=
"make-base-classes-abstract-and-without-data">1. Make base classes abstract and without data
</a></h2>
333 <p>This has the following advantages:
</p>
335 <ol class=
"loweralpha simple">
336 <li>It reduces
<em>coupling
</em> because you do not have to maintain or update state
</li>
339 <ol class=
"loweralpha simple" start=
"2">
340 <li>It helps you to avoid
<em>slicing
</em></li>
343 <ol class=
"loweralpha simple" start=
"3">
344 <li>It ensures you
<em>override
</em> the right function
</li>
347 <p>You might also want to read the following articles:
</p>
349 <li>Kevlin Henney's
<a class=
"reference" href=
"http://www.two-sdg.demon.co.uk/curbralan/papers/SixOfTheBest.pdf">Six of the best
</a></li>
352 <li>Jack Reeves'
<a class=
"reference" href=
"http://www.ddj.com/documents/s=10011/q=1/cuj0602reeves/0602reeves.html">Multiple Inheritance Considered Useful
</a></li>
355 <div class=
"section">
356 <h2><a class=
"toc-backref" href=
"#id11" id=
"make-virtual-functions-private-and-provide-a-non-virtual-public-forwarding-function" name=
"make-virtual-functions-private-and-provide-a-non-virtual-public-forwarding-function">2. Make virtual functions private and provide a non-virtual public forwarding function
</a></h2>
358 <pre class=
"literal-block">
362 virtual int do_foo() =
0;
372 <p>This has the following advantages:
</p>
374 <ol class=
"loweralpha simple">
375 <li>It makes sure all calls to the virtual function always goes through one place in your code
</li>
378 <ol class=
"loweralpha simple" start=
"2">
379 <li>It enables you to check preconditions and postconditions inside the forwarding function
</li>
382 <p>You might also want to read Herb Sutter's article
<a class=
"reference" href=
"http://www.gotw.ca/publications/mill18.htm">Virtuality
</a>.
</p>
384 <div class=
"section">
385 <h2><a class=
"toc-backref" href=
"#id12" id=
"derive-your-base-class-from-boost-noncopyable" name=
"derive-your-base-class-from-boost-noncopyable">3. Derive your base class from
<tt class=
"docutils literal docutils literal"><span class=
"pre">boost::noncopyable
</span></tt></a></h2>
386 <p>Having an abstact base class prevents slicing when the base class is involved, but
387 it does not prevent it for classes further down the hierarchy. This is where
388 <a class=
"reference" href=
"http://www.boost.org/libs/utility/utility.htm#Class_noncopyable">boost::noncopyable
</a> is handy to use:
</p>
389 <pre class=
"literal-block">
390 class Polymorphic : boost::noncopyable
396 <div class=
"section">
397 <h2><a class=
"toc-backref" href=
"#id13" id=
"avoid-null-pointers-in-containers-if-possible" name=
"avoid-null-pointers-in-containers-if-possible">4. Avoid null-pointers in containers (if possible)
</a></h2>
398 <p>By default the pointer containers do not allow you to store null-pointer in them.
399 As you might know, this behavior can be changed explicitly with the use
400 of
<a class=
"reference" href=
"reference.html#class-nullable">boost::nullable
</a>.
</p>
401 <p>The primary reason to avoid null-pointers
402 is that you have to check for null-pointers every time the container is
403 used. This extra checking is easy to forget, and it is somewhat contradictory to
404 the spirit of OO where you replace special cases with dynamic dispatch.
</p>
405 <p>Often, however, you need to place some special object in the container because you
406 do not have enough information to construct a full object. In that case
407 you might be able to use the Null Object pattern which simply dictates that
408 you implement virtual functions from the abstract base-class
409 as empty functions or with dummy return values. This means that
410 your OO-code still does not need to worry about null-pointers.
</p>
411 <p>You might want to read
</p>
413 <li>Kevlin Henney's
<a class=
"reference" href=
"http://www.two-sdg.demon.co.uk/curbralan/papers/europlop/NullObject.pdf">Null Object - Something for Nothing
</a></li>
415 <p>Finally you might end up in a situation where not even the Null Object can help
416 you. That is when you truly need
<tt class=
"docutils literal"><span class=
"pre">container
<</span> <span class=
"pre">nullable
<T
></span> <span class=
"pre">></span></tt>.
</p>
417 <hr><p><strong>Navigate:
</strong></p>
419 <li><a class=
"reference" href=
"ptr_container.html">home
</a></li>
420 <li><a class=
"reference" href=
"reference.html">reference
</a></li>
422 <hr><table class=
"docutils field-list" frame=
"void" rules=
"none">
423 <col class=
"field-name" />
424 <col class=
"field-body" />
426 <tr class=
"field"><th class=
"field-name">Copyright:
</th><td class=
"field-body">Thorsten Ottosen
2004-
2006. Use, modification and distribution is subject to the Boost Software License, Version
1.0 (see
<a class=
"reference" href=
"http://www.boost.org/LICENSE_1_0.txt">LICENSE_1_0.txt
</a>).
</td>