forked from boostorg/smart_ptr
Initial checkin
[SVN r10221]
This commit is contained in:
21
shared_ptr_example2.cpp
Normal file
21
shared_ptr_example2.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
// Boost shared_ptr_example2 implementation file -----------------------------//
|
||||||
|
|
||||||
|
#include "shared_ptr_example2.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class example::implementation
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
~implementation() { std::cout << "destroying implementation\n"; }
|
||||||
|
};
|
||||||
|
|
||||||
|
example::example() : _imp( new implementation ) {}
|
||||||
|
example::example( const example & s ) : _imp( s._imp ) {}
|
||||||
|
|
||||||
|
example & example::operator=( const example & s )
|
||||||
|
{ _imp = s._imp; return *this; }
|
||||||
|
|
||||||
|
void example::do_something()
|
||||||
|
{ std::cout << "use_count() is " << _imp.use_count() << "\n"; }
|
||||||
|
|
||||||
|
example::~example() {}
|
27
shared_ptr_example2.hpp
Normal file
27
shared_ptr_example2.hpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// Boost shared_ptr_example2 header file -----------------------------------//
|
||||||
|
|
||||||
|
#include <boost/smart_ptr.hpp>
|
||||||
|
|
||||||
|
// This example demonstrates the handle/body idiom (also called pimpl and
|
||||||
|
// several other names). It separates the interface (in this header file)
|
||||||
|
// from the implementation (in shared_ptr_example2.cpp).
|
||||||
|
|
||||||
|
// Note that even though example::implementation is an incomplete type in
|
||||||
|
// some translation units using this header, shared_ptr< implementation >
|
||||||
|
// is still valid because the type is complete where it counts - in the
|
||||||
|
// shared_ptr_example2.cpp translation unit where functions requiring a
|
||||||
|
// complete type are actually instantiated.
|
||||||
|
|
||||||
|
class example
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
example();
|
||||||
|
~example();
|
||||||
|
example( const example & );
|
||||||
|
example & operator=( const example & );
|
||||||
|
void do_something();
|
||||||
|
private:
|
||||||
|
class implementation;
|
||||||
|
boost::shared_ptr< implementation > _imp; // hide implementation details
|
||||||
|
};
|
||||||
|
|
15
shared_ptr_example2_test.cpp
Normal file
15
shared_ptr_example2_test.cpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// Boost shared_ptr_example2_test main program ------------------------------//
|
||||||
|
|
||||||
|
#include "shared_ptr_example2.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
example a;
|
||||||
|
a.do_something();
|
||||||
|
example b(a);
|
||||||
|
b.do_something();
|
||||||
|
example c;
|
||||||
|
c = a;
|
||||||
|
c.do_something();
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user