Re: C++ user-defined 'new' (expert question)
That's tough, man...
About overloading operator new: AFAIK, this is how it's done in C++:
struct S
{
void* operator new(size_t, TYPE1 param1, TYPE2 param2) { ... }
S(TYPE param) { ... }
};
and then
S* p = new(newParam1Value, newParam2value) S(ctorParamValue);
IOW, "new" parameters parameters really are for "new", not for
constructor.
(Construct above may be too much for your client? IMO, it's ugly and
obscure enough feature not to use it as a matter of fact and to avoid
it in "line of business" code).
That said, DEBUG_NEW of MFC additionally breaks things. If one does
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
then
S* p = new(newParam1Value, ...
is broken in debug build, by the preprocessor. One can avoid this by
dropping DEBUG_NEW for such cases, e.g. by temporarily #undef-ing it
around given class definition:
#ifdef _DEBUG
#undef DEBUG_NEW
#define DEBUG_NEW new
#endif
class XYZ {...};
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
But then, nice MFC leak reports are gone. CRT leak reports stay,
though.
(Note 2: by C++ rules with exceptions, which are likely irrelevant
here, we also need an overloaded operator delete that matches
overloaded new, that's warning C4291).
Since you say that you want to "protect" your C structs, containment
vs derivation may help (may be bad idea for existing C struct inside C
structs, though). That way, you have to repeat the complete C struct
"interface" for "C++" use :-(, and you have to take care of copying
(refcounting? "noncopyable"?), but you can expose e.g. operator
MYPOINT* from CMyPoint for interoperability e.g.
struct CMyPoint
{
operator MYPOINT*() { return *m_pimpl; }
private:
MYPOINT* m_pimpl;
// refcount?
};
....
void CMyRandomClass::DoSomething(CMyPoint * x)
{ C_API_do_something(x); }
Goran.