Is it required in standard for class specific new, new[], delete, and delete[] to be static. Can i make them non-static member operators. And why is it required for them to be static
+10
A:
Yes it's required for them to be static. They are used to allocate memory for an object that does not yet exist hence there is no instance to refer to.
JaredPar
2010-01-26 17:01:45
Exactly. Otherwise we would have a bonafide chicken and egg problem!
Jason
2010-01-26 17:04:09
I guess this begs the question, why would you want a non-static class-specific `operator new`?
John Dibling
2010-01-26 17:19:57
I was confused because i had written a small program with class specific new without static. But as explained by @apoluektov it is implicitly declared static
Yogesh Arora
2010-01-26 17:22:01
@John: I can imagine some slightly horrible graph node class, where you might want to use a non-static new operator to create a child of this node, and somehow allocate it differently according to the parent (perhaps allocate it from the same cell allocator, to improve locality of reference?). You can always achieve the same thing with a new operator taking an extra parameter. But then you can always achieve the same thing as any non-virtual non-static member function using a static member function with an extra parameter, doesn't mean we don't "want" non-static member functions...
Steve Jessop
2010-01-26 17:25:47
@Steve: Ah, but in that case, `new` is doing more than just allocating memory. One might argue that the `new` operator itself has too many jobs in that case.
John Dibling
2010-01-26 18:41:40
Sure, and the solution is a non-static factory member function. But if `new` could be non-static, I doubt we'd have C++ gurus intoning "never use non-static new, it's completely broken and unnecessary". It is unnecessary, but as I say that doesn't mean you'd *never* want it.
Steve Jessop
2010-01-27 01:08:06
It's obvious that new and new[] must be static. But what about delete? It makes perfect sense for it to be virtual. Let's say you have a operator new in a class B, which inherits from A, now casting B to A (which is valid polymorphism), deleteing the object should still call B's delete (to match B's new, which might be doing something weird) but it wont if delete is static. What am I missing?
roe
2010-02-16 09:56:45
+4
A:
They are declared static implicitly -- even if you didn't type "static".
Alexander Poluektov
2010-01-26 17:10:14
is that true....can somebody confirm this , Maybe this is what i was doing and complier was making it static.
Yogesh Arora
2010-01-26 17:12:53
Yeah, C++ Standard ANSI ISO, 12.5/1: "Any allocation function for a class T is a static member (even if not explicitly declared static)."The same for deallocation in 12.5/6.
Alexander Poluektov
2010-01-26 17:16:56
Thanks i tested it out, I intentionally wrote a program with compilation error in operator new and i did not use static with that. GCC gave me this error `In static member function `static void* Foo::operator new(size_t)':`
Yogesh Arora
2010-01-26 17:24:29