vendredi 31 juillet 2015

where does the pointer created by new get deleted in this code?

I am reading the IB api C++ code, and have found the following class structure

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
};

I feel very uncomfortable about initializing EPosixClientSocket with this, but somehow I can not articulate what exactly the mistake is.

  1. An question to ask is that where the pointer m_pClient, and pointer m_pEWrapper get deleted respectively?
  2. Is there something wrong in this code? If yes, what is it? If not, is it a good practice to write code like this?

Aucun commentaire:

Enregistrer un commentaire