enable_share_from_this功能介绍
2013-12-10 16:35
357 查看
http://www.2cto.com/kf/201212/175430.html
这个类很有意思,让一个被shared_ptr管理生命周期的类能够在自己的成员函数内部访问shared_ptr。有点绕。
举个例子,下面的代码在函数f内部通过this构造了shared_ptr对象,然后打印x_的值。
[cpp]
class B {
public:
B(): x_(4) {
cout << "B::B()" << endl;
}
~B() {
cout << "B::~B()" << endl;
}
void f() {
shared_ptr<B> p(this);
cout << p->x_ << endl;
//shared_from_this();
}
private:
int x_;
};
/*
*
*/
int main(int argc, char** argv) {
shared_ptr<B> x(new B);
x->f();
return 0;
}
编译通过,但是运行结果:
[cpp]
B::B()
4
B::~B()
B::~B()
两次析构B对象,这是个灾难。
现在试一下enable_shared_from_this:
[cpp]
class A : public enable_shared_from_this<A> {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl;
}
void f() {
//cout << shared_from_this()->x_ << endl; // this way is okay too
shared_ptr<A> p = shared_from_this();
cout << p->x_ << endl;
}
private:
int x_;
};
/*
*
*/
int main(int argc, char** argv) {
shared_ptr<A> x(new A);
x->f();
return 0;
}
运行结果:
[cpp]
A::A()
0
A::~A()
那么,为什么需要这样做呢?在自己的类里面访问自己的成员,其实只是个示例代码,一定必要都没有。
不过有一种可能,就是f函数需要返回自己的指针给调用者,难道这样写么?
[cpp]
A* f();
一个裸指针返回出去,失控了。谁也不知道调用者会干什么?
比较聪明的方法是设计成:
shared_ptr<A> f()
好了,这就是为什么我们需要enable_shared_from_this。
这个类很有意思,让一个被shared_ptr管理生命周期的类能够在自己的成员函数内部访问shared_ptr。有点绕。
举个例子,下面的代码在函数f内部通过this构造了shared_ptr对象,然后打印x_的值。
[cpp]
class B {
public:
B(): x_(4) {
cout << "B::B()" << endl;
}
~B() {
cout << "B::~B()" << endl;
}
void f() {
shared_ptr<B> p(this);
cout << p->x_ << endl;
//shared_from_this();
}
private:
int x_;
};
/*
*
*/
int main(int argc, char** argv) {
shared_ptr<B> x(new B);
x->f();
return 0;
}
编译通过,但是运行结果:
[cpp]
B::B()
4
B::~B()
B::~B()
两次析构B对象,这是个灾难。
现在试一下enable_shared_from_this:
[cpp]
class A : public enable_shared_from_this<A> {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl;
}
void f() {
//cout << shared_from_this()->x_ << endl; // this way is okay too
shared_ptr<A> p = shared_from_this();
cout << p->x_ << endl;
}
private:
int x_;
};
/*
*
*/
int main(int argc, char** argv) {
shared_ptr<A> x(new A);
x->f();
return 0;
}
运行结果:
[cpp]
A::A()
0
A::~A()
那么,为什么需要这样做呢?在自己的类里面访问自己的成员,其实只是个示例代码,一定必要都没有。
不过有一种可能,就是f函数需要返回自己的指针给调用者,难道这样写么?
[cpp]
A* f();
一个裸指针返回出去,失控了。谁也不知道调用者会干什么?
比较聪明的方法是设计成:
shared_ptr<A> f()
好了,这就是为什么我们需要enable_shared_from_this。
相关文章推荐
- enable_share_from_this功能介绍
- enable_share_from_this功能介绍
- enable_share_from_this功能介绍
- enable_share_from_this
- 为何会有enable_share_from_this
- share_ptr 与enable_shared_from_this
- 为何会有enable_share_from_this
- 为何会有enable_share_from_this
- enable_share_from_this 模板类使用和完全解析
- enable_share_from_this
- enable_share_from_this
- [loadFromRemoteSources]This release of the .NET Framework does not enable CAS policy by default
- Multiple Inheritance and enable_shared_from_this
- boost 库 enable_shared_from_this 实现原理分析
- enable_shared_from_this使用
- enable_from_this方法的使用与陷阱
- 关于boost中enable_shared_from_this类的原理分析
- This application is currently offline. To enable the application, remove the app_offline.htm file from the application
- 用enable_shared_from_this在类的成员函数中获得指向当向前对象的shared_ptr
- boost shared_ptr: enable_shared_from_this验证