boost::shared_ptr是boost库中用来管理指针的模板,使用它需要#include
第一,boost::shared_ptr管理的指针所指向的对象必须在堆中,因为该模板会在对象离开作用域后调用delete方法,如果对象位于栈中,程序编译能通过,但在运行中会崩溃。另外改模板提供了swap方法,可以让两个模板指针相互交换所指向的对象。
#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
typedef 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"; }
} foo_ptr_ops;
foo_ptr_ops ins_foo_ptr_ops1 = foo_ptr_ops();
FooPtrOps *ins_foo_ptr_ops2 = new FooPtrOps();
FooPtrOps *ins_foo_ptr_ops3 = new foo_ptr_ops;
int main()
{
{
std::vector
std::set
FooPtr foo\_ptr( new Foo( ) );
foo\_vector.push\_back( foo\_ptr );
foo\_set.insert( foo\_ptr );
foo\_ptr.reset( new Foo( ) );
foo\_vector.push\_back( foo\_ptr );
foo\_set.insert( foo\_ptr );
foo\_ptr.reset( new Foo( ) );
foo\_vector.push\_back( foo\_ptr );
foo\_set.insert( foo\_ptr );
foo\_ptr.reset ( new Foo( ) );
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(), ins\_foo\_ptr\_ops1 );
std::cout << "\\nfoo\_set:\\n";
std::for\_each( foo\_set.begin(), foo\_set.end(), \*ins\_foo\_ptr\_ops3 );
FooPtr foo\_ptr1( new Foo( ) );
FooPtr foo\_ptr2( new Foo( ) );
std::cout << "foo\_ptr1: " << foo\_ptr1->x << '\\n';
std::cout << "foo\_ptr2: " << foo\_ptr2->x << '\\n';
foo\_ptr1.swap(foo\_ptr2);
std::cout << "After swap:\\n";
std::cout << "foo\_ptr1: " << foo\_ptr1->x << '\\n';
std::cout << "foo\_ptr2: " << foo\_ptr2->x << '\\n';
foo\_ptr2.swap(foo\_ptr1);
std::cout << "Swap again:\\n";
std::cout << "foo\_ptr1: " << foo\_ptr1->x << '\\n';
std::cout << "foo\_ptr2: " << foo\_ptr2->x << '\\n';
int a = ;
int b\[\] = {, , , };
int \*c = new int();
int \*d = new int\[\];
/\*
\* Because variable a and b are on stack, while boost::shared\_ptr will call delete method,
\* the following two rows of code will cause error.
\*/
// boost::shared_ptr
// boost::shared_ptr
boost::shared_ptr
boost::shared_ptr
std::cout << "bsc: " << *bsc << " bsd: " << *bsd << std::endl;
std::cout << "The variable field finished." << "\\n";
}
int *c = new int();
int *d = new int[];
boost::shared_ptr
boost::shared_ptr
std::cout << "bsc: " << *bsc << " bsd: " << *bsd << std::endl;
std::cout << "\nProgram done.\n";
}
程序的运行结果:
foo_vector:
foo_set:
foo_ptr1:
foo_ptr2:
After swap:
foo_ptr1:
foo_ptr2:
Swap again:
foo_ptr1:
foo_ptr2:
bsc: bsd:
The variable field finished.
Destructing a Foo with x=
Destructing a Foo with x=
Destructing a Foo with x=
Destructing a Foo with x=
Destructing a Foo with x=
Destructing a Foo with x=
bsc: bsd:
Program done.
第二,boost::shared_ptr支持隐藏类的定义。如下面的代码中,class implementation的定义可以放置于另一个源文件中,在利用boost::shared_ptr管理implementation类型的指针变量时,可以先声明一下类implementation,然后就能定义boost::shared_ptr< implementation >类型的指针变量。
#include
#include
#include
void print_val(int v)
{
std::cout << v << " ";
}
class example
{
public:
example();
void do_something();
int val[];
class implementation;
boost::shared_ptr< implementation > _imp; // hide implementation details
};
class example::implementation
{
public:
~implementation() { std::cout << "destroying implementation\n"; }
};
example::example() : _imp( new implementation ) {}
void example::do_something()
{
std::cout << "use_count() is " << _imp.use_count() << " ";
std::for_each(val, val + , print_val);
std::cout << "\n";
}
int main()
{
example a;
a.val[] = ;
a.val[] = ;
a.val[] = ;
a.do_something();
example b(a);
b.do_something();
example c;
c = a;
a.do_something();
b.do_something();
c.do_something();
return ;
}
程序的运行结果:
use_count() is
use_count() is
destroying implementation
use_count() is
use_count() is
use_count() is
destroying implementation
第三,使用boost::shared_ptr提供的reset()方法,可以使boost::shared_ptr管理的指针所指向的对象的引用计数减一。当所指对象的引用计数减至0时,所指对象的析构函数将被调用,所指对象被销毁。
#include
#include
#include
using namespace std;
class Book
{
private:
string name_;
public:
Book(string name) : name_(name)
{
cout << "Creating book " << name_ << " …" << endl;
}
~Book()
{
cout << "Destroying book " << name_ << " …" << endl;
}
};
int main()
{
cout << "=====Main Begin=====" << endl;
{
boost::shared_ptr
cout << "myBook1: " << myBook1.use_count() << endl;
boost::shared_ptr
cout << "myBook1: " << myBook1.use_count() << endl;
boost::shared_ptr
myBook3 = myBook1;
cout << "\\n\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n";
cout << "myBook1: " << myBook1.use\_count() << endl;
cout << "myBook2: " << myBook2.use\_count() << endl;
cout << "myBook3: " << myBook3.use\_count() << endl;
cout << "\\n\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n";
myBook1.reset();
cout << "myBook1: " << myBook1.use\_count() << endl;
cout << "myBook2: " << myBook2.use\_count() << endl;
cout << "myBook3: " << myBook3.use\_count() << endl;
cout << "\\n\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n";
myBook3.reset();
cout << "myBook1: " << myBook1.use\_count() << endl;
cout << "myBook2: " << myBook2.use\_count() << endl;
cout << "myBook3: " << myBook3.use\_count() << endl;
cout << "\\n\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n";
myBook2.reset();
cout << "myBook1: " << myBook1.use\_count() << endl;
cout << "myBook2: " << myBook2.use\_count() << endl;
cout << "myBook3: " << myBook3.use\_count() << endl;
cout << "After reset ..." << endl;
}
cout << "===== Main End =====" << endl;
return ;
}
程序的运行结果:
=====Main Begin=====
Creating book 「」 …
myBook1:
myBook1:
****************************
myBook1:
myBook2:
myBook3:
****************************
myBook1:
myBook2:
myBook3:
****************************
myBook1:
myBook2:
myBook3:
****************************
Destroying book 「」 …
myBook1:
myBook2:
myBook3:
After reset …
===== Main End =====
手机扫一扫
移动阅读更方便
你可能感兴趣的文章