目录
scoped_ptr
代码:
#include <iostream>
#include <boost/scoped_ptr.hpp>
using namespace std;
class X
{
public:
X() { cout << "X..." << endl; }
~X() { cout << "~X..." << endl; }
};
int main()
{
cout << "Entering main..." << endl;
{
boost::scoped_ptr<X> p(new X);
//boost::scoped_ptr<X> p2(p); //不能拷贝和赋值
}
cout << "Exiting main..." << endl;
return 0;
}
运行结果:
shared_ptr
代码:
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
class X
{
public:
X() { cout << "X..." << endl; }
~X() { cout << "~X..." << endl; }
};
int main()
{
cout << "Entering main..." << endl;
boost::shared_ptr<X> p1(new X);
cout << p1.use_count() << endl;
boost::shared_ptr<X> p2 = p1;
cout << p1.use_count() << endl;
p1.reset();
cout << p2.use_count() << endl;
p2.reset();
cout << "Exiting main..." << endl;
return 0;
}
运行结果:
跟踪调试内部实现看不懂。涉及到原子性自增,线程安全。
以及怎么"修改一个常量"(用什么引用技术)?
shared_ptr与STL
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
using namespace std;
class X
{
public:
X() { cout << "X..." << endl; }
~X() { cout << "~X..." << endl; }
};
int main()
{
vector<auto_ptr<X>> v;
auto_ptr<X> p(new X);
v.push_back(p);
return 0;
}
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
using namespace std;
class X
{
public:
X() { cout << "X..." << endl; }
~X() { cout << "~X..." << endl; }
};
int main()
{
//vector<auto_ptr<X>> v;
//auto_ptr<X> p(new X);
//v.push_back(p);
vector<boost::shared_ptr<X>> vv;
boost::shared_ptr<X> p(new X);
vv.push_back(p);
cout << p.use_count() << endl;
return 0;
}
循环引用
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
class Parent;
class Child;
typedef boost::shared_ptr<Parent> parent_ptr;
typedef boost::shared_ptr<Child> child_ptr;
class Parent
{
public:
Parent() { cout << "Parent..." << endl; }
~Parent() { cout << "~Parent..." << endl; }
child_ptr child_;
};
class Child
{
public:
Child() { cout << "Child..." << endl; }
~Child() { cout << "~Child..." << endl; }
parent_ptr parent_;
};
int main()
{
parent_ptr parent(new Parent);
child_ptr child(new Child);
parent->child_ = child;
child->parent_ = parent;
return 0;
}
运行结果发现没有调用析构函数。
解决办法(打破循环引用)
1 手动打破循环引用
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
class Parent;
class Child;
typedef boost::shared_ptr<Parent> parent_ptr;
typedef boost::shared_ptr<Child> child_ptr;
class Parent
{
public:
Parent() { cout << "Parent..." << endl; }
~Parent() { cout << "~Parent..." << endl; }
child_ptr child_;
};
class Child
{
public:
Child() { cout << "Child..." << endl; }
~Child() { cout << "~Child..." << endl; }
parent_ptr parent_;
};
int main()
{
parent_ptr parent(new Parent);
child_ptr child(new Child);
parent->child_ = child;
child->parent_ = parent;
parent->child_.reset(); // xxx
return 0;
}
2 弱引用weak_ptr
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp> // xxx
using namespace std;
class Parent;
class Child;
typedef boost::shared_ptr<Parent> parent_ptr;
typedef boost::shared_ptr<Child> child_ptr;
class Parent
{
public:
Parent() { cout << "Parent..." << endl; }
~Parent() { cout << "~Parent..." << endl; }
//child_ptr child_;
boost::weak_ptr<Child> child_; // xxx
};
class Child
{
public:
Child() { cout << "Child..." << endl; }
~Child() { cout << "~Child..." << endl; }
parent_ptr parent_;
};
int main()
{
parent_ptr parent(new Parent);
child_ptr child(new Child);
parent->child_ = child;
child->parent_ = parent;
//parent->child_.reset(); // xxx
return 0;
}
注意事项:
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <vector>
using namespace std;
class X
{
public:
X() { cout << "X..." << endl; }
~X() { cout << "~X..." << endl; }
void Fun() { cout << "Fun..." << endl; }
};
int main()
{
boost::weak_ptr<X> p;
{
boost::shared_ptr<X> p2(new X);
cout << p2.use_count() << endl;
p = p2;
cout << p2.use_count() << endl;
boost::shared_ptr<X> p3 = p.lock();
if (!p3)
cout << "object is destroyed" << endl;
else
p3->Fun();
}
boost::shared_ptr<X> p4 = p.lock();
if (!p4)
cout << "object is destroyed" << endl;
else
p4->Fun();
return 0;
}
scoped_array/shared_array
#include <iostream>
#include <boost/scoped_array.hpp>
using namespace std;
class X
{
public:
X() { cout << "X..." << endl; }
~X() { cout << "~X..." << endl; }
};
int main()
{
boost::scoped_array<X> p(new X[3]);
return 0;
}