Changeset 54637


Ignore:
Timestamp:
Jul 4, 2009, 9:37:15 AM (9 years ago)
Author:
cschladetsch
Message:

udpated

Location:
sandbox/cloneable
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • sandbox/cloneable/libs/cloneable/doc/cloneable.qbk

    r54636 r54637  
    122122    virtual string get_name() const = 0;
    123123};
     124``
     125
     126Note that `Animal` doesn't have to derive from anything, or have any special members or methods.
     127Now let's use out base class to create a simple type hierarchy:
     128
     129``
    124130
    125131class Cat : public cloneable::base<Cat, Animal>
     
    132138    string get_name() const { return name; }
    133139};
    134 
     140``
     141
     142We derive our `Cat` class from `cloneable::base<Cat, Animal>`. The first type argument to `base<...>` is
     143the derived type to use for cloning, and the second argument is the common base type of the hierarhcy.
     144Other than that, `Cat` implements the `get_name` pure virtual method derived in the base class `Animal`.
     145
     146Now, let's use it:
     147
     148``
    135149int main()
    136150{
     
    149163``
    150164
    151 In this example, we note that the `clone()` method returns
     165Note that the `clone()` method returns
    152166a pointer to the base class of the type hierarchy - in this case, `Animal`.
    153167We had to use a dynamic cast to down-cast to our derived type.
     
    172186{
    173187};
    174 
     188``
     189
     190`Dog` is just like a cat, but different. `Labrador` is the first example we have seen
     191that uses multiple inheritance. In this case, we are saying that a Labrador is-a Dog,
     192and is Cloneable as either a Dog or a Labrador. Using these types:
     193
     194``
    175195int main()
    176196{
     
    187207
    188208Here we see that class Labrador is-a Dog, and is also cloneable as a Labrador.
    189 When cloning this class, we specify which sub-object we wish to duplicate. Note that
     209When cloning this class, we specify which sub-object we wish to duplicate.
    190210when using `clone_as<T>`, we actually are making a type of T, rather than making another type and casting up.
    191211
     
    229249struct Base { virtual ~Base() {} };
    230250
    231 struct T0 : cloneable::base<T0, Base, no_default_construction>
     251struct T0 : cloneable::base<T0, Base, no_default_construction_tag>
    232252{
    233253    string &str;
    234254    T0(string &s) : str(s) { }
    235255};
    236 
     256``
     257
     258`T0` has a member that is a reference and as such doesn't have a default constructor.
     259To avoid compile-time errors when using this type in the Cloneable libray, you must
     260indicate that it is not default-constructible by passing the `no_default_construction_tag`
     261type as a parameter to cloneable::base<>. The order that you give the base-type or tag-types
     262to cloneable::base<> is not important and is dealt with be the library.
     263
     264Putting this type to use:
     265
     266``
    237267int main()
    238268{
     
    259289``
    260290
    261 Note that we had to pass the `no_default_construction` tag-type to `cloneable::base<>` in
    262 the definition of T0. Without this, an error would occur when compiling the `create_new()` line.
    263 
    264 The `no_default_construction` tag is thrown if you attempt to default-construct a
     291A `no_default_construction` exception is thrown if you attempt to default-construct a
    265292type that has no default constructor.
    266293
  • sandbox/cloneable/libs/cloneable/doc/html/cloneable/tutorial/simple_hierarchical_cloning.html

    r54627 r54637  
    4949    <span class="keyword">virtual</span> <span class="identifier">string</span> <span class="identifier">get_name</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    5050<span class="special">};</span>
    51 
    52 <span class="keyword">class</span> <span class="identifier">Cat</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">cloneable</span><span class="special">::</span><span class="identifier">base</span><span class="special">&lt;</span><span class="identifier">Cat</span><span class="special">,</span> <span class="identifier">Animal</span><span class="special">&gt;</span>
     51</pre>
     52<p>
     53      </p>
     54<p>
     55        Note that <code class="computeroutput"><span class="identifier">Animal</span></code> doesn't
     56        have to derive from anything, or have any special members or methods. Now
     57        let's use out base class to create a simple type hierarchy:
     58      </p>
     59<p>
     60       
     61</p>
     62<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">Cat</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">cloneable</span><span class="special">::</span><span class="identifier">base</span><span class="special">&lt;</span><span class="identifier">Cat</span><span class="special">,</span> <span class="identifier">Animal</span><span class="special">&gt;</span>
    5363<span class="special">{</span>
    5464    <span class="identifier">string</span> <span class="identifier">name</span><span class="special">;</span>
     
    5969    <span class="identifier">string</span> <span class="identifier">get_name</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">name</span><span class="special">;</span> <span class="special">}</span>
    6070<span class="special">};</span>
    61 
    62 <span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
     71</pre>
     72<p>
     73      </p>
     74<p>
     75        We derive our <code class="computeroutput"><span class="identifier">Cat</span></code> class from
     76        <code class="computeroutput"><span class="identifier">cloneable</span><span class="special">::</span><span class="identifier">base</span><span class="special">&lt;</span><span class="identifier">Cat</span><span class="special">,</span> <span class="identifier">Animal</span><span class="special">&gt;</span></code>. The first type argument to <code class="computeroutput"><span class="identifier">base</span><span class="special">&lt;...&gt;</span></code>
     77        is the derived type to use for cloning, and the second argument is the common
     78        base type of the hierarhcy. Other than that, <code class="computeroutput"><span class="identifier">Cat</span></code>
     79        implements the <code class="computeroutput"><span class="identifier">get_name</span></code> pure
     80        virtual method derived in the base class <code class="computeroutput"><span class="identifier">Animal</span></code>.
     81      </p>
     82<p>
     83        Now, let's use it:
     84      </p>
     85<p>
     86       
     87</p>
     88<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
    6389<span class="special">{</span>
    6490    <span class="identifier">Cat</span> <span class="special">*</span><span class="identifier">cat</span> <span class="special">=</span> <span class="keyword">new</span> <span class="identifier">Cat</span><span class="special">(</span><span class="string">"sam"</span><span class="special">);</span>
     
    78104      </p>
    79105<p>
    80         In this example, we note that the <code class="computeroutput"><span class="identifier">clone</span><span class="special">()</span></code> method returns a pointer to the base class
    81         of the type hierarchy - in this case, <code class="computeroutput"><span class="identifier">Animal</span></code>.
    82         We had to use a dynamic cast to down-cast to our derived type.
     106        Note that the <code class="computeroutput"><span class="identifier">clone</span><span class="special">()</span></code>
     107        method returns a pointer to the base class of the type hierarchy - in this
     108        case, <code class="computeroutput"><span class="identifier">Animal</span></code>. We had to use
     109        a dynamic cast to down-cast to our derived type.
    83110      </p>
    84111<p>
     
    112139<span class="special">{</span>
    113140<span class="special">};</span>
    114 
    115 <span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
     141</pre>
     142<p>
     143      </p>
     144<p>
     145        <code class="computeroutput"><span class="identifier">Dog</span></code> is just like a cat, but
     146        different. <code class="computeroutput"><span class="identifier">Labrador</span></code> is the
     147        first example we have seen that uses multiple inheritance. In this case,
     148        we are saying that a Labrador is-a Dog, and is Cloneable as either a Dog
     149        or a Labrador. Using these types:
     150      </p>
     151<p>
     152       
     153</p>
     154<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
    116155<span class="special">{</span>
    117156    <span class="identifier">Labrador</span> <span class="special">*</span><span class="identifier">lab</span> <span class="special">=</span> <span class="keyword">new</span> <span class="identifier">Labrador</span><span class="special">();</span>
     
    130169        Here we see that class Labrador is-a Dog, and is also cloneable as a Labrador.
    131170        When cloning this class, we specify which sub-object we wish to duplicate.
    132         Note that when using <code class="computeroutput"><span class="identifier">clone_as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>,
     171        when using <code class="computeroutput"><span class="identifier">clone_as</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>,
    133172        we actually are making a type of T, rather than making another type and casting
    134173        up.
  • sandbox/cloneable/libs/cloneable/doc/html/index.html

    r54636 r54637  
    7575</div>
    7676<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
    77 <td align="left"><p><small>Last revised: July 04, 2009 at 09:17:12 GMT</small></p></td>
     77<td align="left"><p><small>Last revised: July 04, 2009 at 09:34:23 GMT</small></p></td>
    7878<td align="right"><div class="copyright-footer"></div></td>
    7979</tr></table>
  • sandbox/cloneable/libs/cloneable/test/cloneable.vcproj

    r54624 r54637  
    278278                                </File>
    279279                                <File
     280                                        RelativePath="..\..\..\boost\cloneable\cloneable.hpp"
     281                                        >
     282                                </File>
     283                                <File
    280284                                        RelativePath="..\..\..\boost\cloneable\forward_declarations.hpp"
    281285                                        >
Note: See TracChangeset for help on using the changeset viewer.