A question regarding operator new
Here is the code,
Code:
class A
{
public:
A(int x) {}
static void* operator new(size_t size){}
};
int main()
{
void* rawMemory = operator new[](10*sizeof(A));
A* pA = static_cast<A*>(rawMemory);
for(int i=0;i<10;i++)
new(&pA[i]) A(i);
for(int j=9;j>=0;j--)
pA[j].~A();
operator delete[](rawMemory);
return 0;
}
There is compiler error "error C2660: 'A::operator new' : function does not take 2 arguments". What does it mean? Also one more question here. What if overloaded operator new is not static? Is there any problem with operator new not being static or not being global? Thanks.
Re: A question regarding operator new
Change the code like this
Code:
#include<iostream>
using namespace std;
class A
{
public:
A(int x) {}
static void* operator new[](size_t size){}
};
int main()
{
void* rawMemory = operator new[](10*sizeof(A));
A* pA = static_cast<A*>(rawMemory);
for(int i=0;i<10;i++)
new(&pA[i]) A(i);
for(int j=9;j>=0;j--)
pA[j].~A();
operator delete[](rawMemory);
return 0;
}
Re: A question regarding operator new
In all my years of programming, so far I have never needed to overload operator new.
You might be better off focusing your studies elsewhere.
Re: A question regarding operator new
2 things:
I believe you need to #include <memory> in order to use placement new.
Also, placement new should take a void*, not an A*.
That said, I agree with Lindley.
Re: A question regarding operator new
Quote:
Originally Posted by Speedo
I believe you need to #include <memory> in order to use placement new.
That would be #include <new>.
Quote:
Originally Posted by Speedo
Also, placement new should take a void*, not an A*.
Pointers to objects are convertible to void*, so that is not a problem.
Re: A question regarding operator new
I truly respect what you believe but I have different philosophy. Sometimes, knowing one thing is probably not beneficial at all. But during the process of knowing one thing, you might know many other things. Someone was trying to invent a satellite but it turns out he invented a IPhone(I made it up but it says all I want to say -- knowledge has no boundary).
Quote:
Originally Posted by
Lindley
In all my years of programming, so far I have never needed to overload operator new.
You might be better off focusing your studies elsewhere.
Re: A question regarding operator new
I don't even include <new> in order to use placement new. If I don't overload operator new, then it compiles fine. Any idea why overloaded operator new causes the compiler error? Thanks.
Quote:
Originally Posted by
laserlight
That would be #include <new>.
Pointers to objects are convertible to void*, so that is not a problem.
Re: A question regarding operator new
Quote:
Originally Posted by
LarryChen
I don't even include <new> in order to use placement new. If I don't overload operator new, then it compiles fine. Any idea why overloaded operator new causes the compiler error? Thanks.
maybe because it's not implemented? How do you expect to allocate anything if your new does nothing?
at the very least, make it return null. at best, have it allocate some, and return the pointer to the allocated space.
Re: A question regarding operator new
Never mind, this is the problem:
the "new operator" calls "operator new", and in particular, "new (...) X" calls "operator new(X, ...)"
The problem is that by providing "operator nrw(size_t)", you are hiding all other operator new that are global, but could be applied to your class. Your compiler is looking for "operator new(size_t size, void* ptr)", but all it finds is "size_t size)". Remember that (Koenig) lookup stops in the first scope that has a name match. From there it tries to find a correct match from all current name match. If this fails, you don't go looking in the parent scope, you error.
You need to add the two other operator new: The inplace one, and the nothrow one:
Code:
class A
{
public:
A(int x) {}
static void* operator new[](size_t size) throw (std::bad_alloc)
{
return ::operator new(size);
}
static void* operator new[](size_t size, const std::nothrow_t& nothrow_constant) throw()
{
return ::operator new(size, nothrow_constant);
}
static void* operator new[](size_t size, void* ptr) throw()
{
return ::operator new(size, ptr);
//return ptr
}
};
Notice though that "inplace operator new" doesn't actually do anything, but return the input pointer. This is because it doesn't have anything to allocate, and it's not its job to construct anything (that would be "new operator"'s job).