All*_*nzi 1 c++ pointers c++11
我正在阅读IB api C++代码,并找到了以下类结构
class EWrapper;
class EClientSocketBase{
public:
EClientSocketBase( EWrapper *ptr): m_pEWrapper(ptr){}
~EClientSocketBase(){ }
// some methods
private:
EWrapper *m_pEWrapper;
// some other data members
};
class EPosixClientSocket : public EClientSocketBase{
// some methods and data members
EPosixClientSocket( EWrapper *ptr) : EClientSocketBase( ptr){}
~EPosixClientSocket(){}
};
class PosixTestClient : public EWrapper{
public:
PosixTestClient(): m_pClient(new EPosixClientSocket(this)){}
~PosixTestClient(){}
// some other methods
private:
std::auto_ptr<EPosixClientSocket> m_pClient;
// some other methods and data members
};
Run Code Online (Sandbox Code Playgroud)
我觉得这个代码非常不舒服,尤其是在不把delete指针m_pEWrapper中的析构函数EClientSocketBase,更不舒服的初始化EPosixClientSocket用this,但不知何故,我不能贴切地描述出错误是什么.
m_pClient和指针m_pEWrapper分别被删除了吗?你应该放入delete m_pEWrapper析构函数EClientSocketBase吗?那std::auto_ptr是工作.它是:
一个智能指针,用于管理通过新表达式获取的对象,并在
auto_ptr自身被销毁时删除该对象.
因此~PosixTestClient(),当m_pClient被破坏时,它将delete是它正在管理的指针.这比照顾delete自己要安全得多.
std::auto_ptr不赞成使用std::unique_ptr,这是一种严格优越的替代方案.
| 归档时间: |
|
| 查看次数: |
153 次 |
| 最近记录: |