我想其他人可能会发现这很有用。这是我今天拼凑的一个快速的小智能指针类,以帮助我避免忘记释放resbuf指针:
- class ResbufPointer {
- resbuf* pHead; // Maintain a pointer to the head of the resource chain for de-allocation
- resbuf* pBuffer;
- public:
- ResbufPointer(resbuf* ptr) : pHead(ptr), pBuffer(ptr) {}
- ~ResbufPointer() {
- acutRelRb(pHead); // release the buffer
- pHead = pBuffer = 0;
- }
- resbuf* operator->() { return pBuffer; } // so you can do things like buf->resval.rstring
- operator resbuf*() { return pBuffer; } // so you can pass the smart pointer to functions that expect a resbuf*
- bool isNull() { return pBuffer == 0; } // null pointer check
- void start() { pBuffer = pHead; } // in case you need to return to the head of the resource chain
- void next() { pBuffer = pBuffer->rbnext; } // try to move the pointer to the next item in the resource chain
- };
在如此琐碎的事情上将实现与接口分开似乎很傻,所以我只是把它们都放在一起。
添加了一些注释。更改了愚蠢的变量名称。
本帖以下内容被隐藏保护;需要你回复后,才能看到! 游客,如果您要查看本帖隐藏内容请 回复 |