Overloading operator new
I am overloading operator new as below
class A {
public:
void* operator new(size_t) { return (void*) Buf; }
};
I am getting "declaration of opera开发者_如何学运维tor new as non-function error" when I try to compile. Could someone help me with this?
Have you size_t
be defined? You need to include stddef.h
for it. But better you include cstddef
and use std::size_t
.
Your declaration otherwise looks fine, apart from the semantics of always returning Buf
being screwed up. The operator new
should return a buffer of the size specified as the first argument.
I'm guessing that you are using a fairly modern and strict compiler. The error you are getting is because size_t
is not recognized. Strictly you need to #include
something that defines it and you should also use the C++ name: std::size_t
.
E.g.
#include <cstddef>
class A{
public:
void* operator new(std::size_t) { return (void*) Buf;}
};
精彩评论