How does one provide a dependency to a class for use in operator new, without using a global?
If I understand correctly, if I want to customize the behavior every time someone creates an instance of my type, then I have to overload operator new as a class method. That class method is static, whether I declare it static or not.
If I have a class:
class ComplexNumber
{
public:
ComplexNumber(double realPart, double complexPart);
ComplexNumber(const ComplexNumber & rhs);
virtual ~ComplexNumber();
void * operator new (std::size_t count);
void * operator new[](std::size_t count);
protected:
double m_realPart;
double m_complexPart;
};
and I want to use an custom memory manager that I created to do the allocation:
void * ComplexNumber::operator new (std::size_t count)
{
// I want to use an call IMemoryManager::allocate(size, align);
}
void * ComplexNumber::operator new[](std::size_t count)
{
// I want to use an call IMemoryManager::allocate(size, align);
}
How do I make an instance of IMemoryManager available to the class without using a global?
It does not seem possible to me, and therefore forces bad design where the class is tightly coupled to a particular global instance.