-
July 28th, 2010, 11:08 AM
#1
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:perator 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.
-
July 28th, 2010, 11:55 AM
#2
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;
}
-
July 28th, 2010, 12:45 PM
#3
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.
-
July 28th, 2010, 04:03 PM
#4
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.
-
July 28th, 2010, 09:55 PM
#5
Re: A question regarding operator new
Originally Posted by Speedo
I believe you need to #include <memory> in order to use placement new.
That would be #include <new>.
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.
-
July 29th, 2010, 07:09 PM
#6
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).
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.
-
July 29th, 2010, 07:14 PM
#7
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.
Originally Posted by laserlight
That would be #include <new>.
Pointers to objects are convertible to void*, so that is not a problem.
-
July 30th, 2010, 01:36 AM
#8
Re: A question regarding operator new
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
July 30th, 2010, 01:45 AM
#9
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).
Last edited by monarch_dodra; July 30th, 2010 at 01:50 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|