The question below can be a general question about design patterns but, due to my lack of knowledge, I couldn't.
I am trying to implement a RAII-style buffer class (Buffer) using a given memory manager class (MemoryManager) with interfaces like open_manager/close_manager/allocate/deallocate etc. The Buffer would look like:
class Buffer {
public:
explicit Buffer(std::size_t sz) { /* Allocation */ }
~Buffer() { /* Deallocation */ }
};
But as a condition, the MemoryManager class needs to be instantiated, which means that the Buffer needs to be able to use the specific manager instance in the destructor.
~Buffer() { manager.dealloc(/* ... */); }
There would be several options:
const MemoryManager*as aBuffermember variable like:class Buffer { public: Buffer(std::size_t sz, const MemoryManager* manager) : manager_(manager) { /*...*/ } ~Buffer() { manager_->dealloc(/*...*/); } private: const MemoryMenager* manager_{nullptr}; };const MemoryManager&as aBuffermember variable- The manager as a global variable
Which options are the best ? or are there any better ways to get this done ?
Thank you.
Aucun commentaire:
Enregistrer un commentaire