I've been playing around with operator new() (as a class member, not global), and I haven't been able to find a lot of useful examples other than the definitions.
I wrote a class with a operator new() function like this:
<code>
void* operator new( size_t size )
{
CDeletable* pTemp = ::new CDeletable;
return pTemp;
};
</code>
I used "::new" to call the global new rather than the same function I'm in. (I'm assuming that's how to call the global new?) Anyways, in the debugger this code does work, however, the constructor is called twice. I'm wondering why that is?
If I change the function like this:
<code>
void* operator new( size_t size )
{
void* pTemp = malloc( size );
return pTemp;
};
</code>
Then the constructor is only called once. I'd really rather avoid using malloc() in a C++ program though. Can I use the first method, or something like it, which only calls the constructor once?
Thanks,
Chris
I wrote a class with a operator new() function like this:
<code>
void* operator new( size_t size )
{
CDeletable* pTemp = ::new CDeletable;
return pTemp;
};
</code>
I used "::new" to call the global new rather than the same function I'm in. (I'm assuming that's how to call the global new?) Anyways, in the debugger this code does work, however, the constructor is called twice. I'm wondering why that is?
If I change the function like this:
<code>
void* operator new( size_t size )
{
void* pTemp = malloc( size );
return pTemp;
};
</code>
Then the constructor is only called once. I'd really rather avoid using malloc() in a C++ program though. Can I use the first method, or something like it, which only calls the constructor once?
Thanks,
Chris