std::map is driving me insane!
Quote:Original post by Zahlmanstruct CTextureHandle { CTexture* ptr; // Look up std::auto_ptr implementation to see how to make operator*, // operator-> etc. work right for your class so that you don't have to // go through the ptr member explicitly. Unfortunately std::auto_ptr // itself doesn't do exactly what you want for the rest of the class... CTextureHandle(args) : ptr(new CTexture(args)) {} ~CTextureHandle() { cleanup(); } // Since dtor is needed, copy ctor and assignment op are too. // I'll assume an appropriate CTexture copy ctor exists: CTextureHandle(const CTextureHandle& other) : ptr(new CTexture(*(other.ptr))) {} CTextureHandle& operator= (const CTextureHandle& rhs) { cleanup(); ptr = new CTexture(*(other.ptr)); } private: void cleanup() { glDeleteTextures(1,&(ptr->texID)); // actually should move that part to the CTexture dtor delete ptr; // nulling post-delete is useless here :) }}// Now use CTextureHandle instead of CTexture* in your map, and then:CTextureMgr::~CTextureMgr(){ // Nothing is needed; ~m_textures will be invoked automatically, which // will destruct all the t_texturePairs that are stored, which in turn // calls ~CTextureHandle() for each of those things in the map.}
Ok, I looked at this a little more closely and it makes sense now. Is this an example of PIMPL?
This seems like a pretty good way to handle my problem. It also seems a lot more straight forward than smart pointers.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement