From ac8d0f55053b04d8b0a0632cd4c8b624141abb24 Mon Sep 17 00:00:00 2001
From: Beman Dawes
One common usage of shared_pointer is to implement a handle/body +
One common usage of scoped_ptr is to implement a handle/body structure which avoids exposing the body (implementation) in the header file:
class handle { @@ -123,7 +123,7 @@ body is not visible at the time scoped_ptr<> deletes it. See ISO 5.3.5/5. Note that some compilers will issue a warning even though the above code is well defined.
-Revised 10 May 2001
+Revised 21 May 2001
© Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" diff --git a/shared_ptr.htm b/shared_ptr.htm index affff12..4dd2b87 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -192,33 +192,17 @@ stored pointer.
void swap( shared_ptr<T>& other ) throw()
Swaps the two smart pointers, as if by std::swap.
Class shared_ptr example
-// The application will produce a series of -// objects of type Foo which later must be -// accessed both by occurrence (std::vector) -// and by ordering relationship (std::set). - -class Foo { ... }; - -typedef boost::shared_ptr<Foo> FooPtr; - -std::vector<FooPtr> foo_vector; -std::set<FooPtr> foo_set; // NOT multiset! - -... -{ // creation loop - FooPtr foo_ptr ( new Foo( ... ) ); - foo_vector.push_back( foo_ptr ); - foo_set.insert( foo_ptr ); -}-Note that at the termination of the creation loop, some of the FooPtr objects -may have use_count()==1 rather than use_count()==2, since foo_set is a std::set -rather than a std::multiset. Furthermore, use_count() will be even higher -at various times inside the loop, as container operations are performed. +
See shared_ptr_example.cpp for a complete example program.
+This program builds a std::vector and std::set of FooPtr's.
+Note that after the two containers have been populated, some of the FooPtr objects +will have use_count()==1 rather than use_count()==2, since foo_set is a std::set +rather than a std::multiset. Furthermore, use_count() may be even higher +at various times while push_back() and insert() container operations are performed. More complicated yet, the container operations may throw exceptions under a variety of circumstances. Without using a smart pointer, memory and exception management would be a nightmare.
-Revised 10 February, 2001 +
Revised 21 May, 2001
© Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright diff --git a/shared_ptr_example.cpp b/shared_ptr_example.cpp new file mode 100644 index 0000000..988efbd --- /dev/null +++ b/shared_ptr_example.cpp @@ -0,0 +1,96 @@ +// Boost shared_ptr_example.cpp --------------------------------------------// + +// (C) Copyright Beman Dawes 2001. Permission to copy, +// use, modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided "as is" +// without express or implied warranty, and with no claim as to its +// suitability for any purpose. + +// See http://www.boost.org for most recent version including documentation. + +// Revision History +// 21 May 01 Initial complete version (Beman Dawes) + +// The original code for this example appeared in the shared_ptr documentation. +// Ray Gallimore pointed out that foo_set was missing a Compare template +// argument, so would not work as intended. At that point the code was +// turned into an actual .cpp file so it could be compiled and tested. + +#include
+#include +#include +#include +#include + +// The application will produce a series of +// objects of type Foo which later must be +// accessed both by occurrence (std::vector) +// and by ordering relationship (std::set). + +struct Foo +{ + Foo( int _x ) : x(_x) {} + ~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; } + int x; + /* ... */ +}; + +typedef boost::shared_ptr FooPtr; + +struct FooPtrOps +{ + bool operator()( const FooPtr & a, const FooPtr & b ) + { return a->x > b->x; } + void operator()( const FooPtr & a ) + { std::cout << a->x << "\n"; } +}; + +int main() +{ + std::vector foo_vector; + std::set foo_set; // NOT multiset! + + FooPtr foo_ptr( new Foo( 2 ) ); + foo_vector.push_back( foo_ptr ); + foo_set.insert( foo_ptr ); + + foo_ptr.reset( new Foo( 1 ) ); + foo_vector.push_back( foo_ptr ); + foo_set.insert( foo_ptr ); + + foo_ptr.reset( new Foo( 3 ) ); + foo_vector.push_back( foo_ptr ); + foo_set.insert( foo_ptr ); + + foo_ptr.reset ( new Foo( 2 ) ); + foo_vector.push_back( foo_ptr ); + foo_set.insert( foo_ptr ); + + std::cout << "foo_vector:\n"; + std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() ); + + std::cout << "\nfoo_set:\n"; + std::for_each( foo_set.begin(), foo_set.end(), FooPtrOps() ); + std::cout << "\n"; + +// Expected output: +// +// foo_vector: +// 2 +// 1 +// 3 +// 2 +// +// foo_set: +// 3 +// 2 +// 1 +// +// Destructing a Foo with x=2 +// Destructing a Foo with x=1 +// Destructing a Foo with x=3 +// Destructing a Foo with x=2 + + return 0; +} +