|
-
June 21st, 2005, 07:39 PM
#1
overriding new
I've implemented a new operator with a parameter in one of my classes as such:
Code:
class test
{
public:
static void* operator new (size_t size, int somevalue)
{
return malloc(size);
}
};
However, because I'm defining a parametered new operator, the default new operator ceases to exist and I get compiler errors if I want to instance an object using the default new.
Is there any way to keep the default new operator or do I have to redefine it manually?
-
June 21st, 2005, 09:06 PM
#2
Re: overriding new
I think you either have to redefine it manually or provide default arguments for all parameters except the size_t.
Hungarian notation is the bane of self documenting code.
Forget all fancy tricks with operator precedence. Code should be easily readable.
May the BOOST be with you.
Good free E-Books thanks to Bruce Eckel.
-
June 21st, 2005, 09:41 PM
#3
Re: overriding new
Beside providing a default operator new, you also have to provide a corresponding operator delete.
Code:
#include <iostream>
using namespace std;
class test
{
public:
test()
{
cout << "test::test()" << endl;
}
~test()
{
cout << "test::~test()" << endl;
}
static void *operator new(size_t size, int value)
{
cout << "placement new" << endl;
return malloc(size);
}
static void *operator new(size_t size)
{
cout << "operator new" << endl;
return ::operator new(size);
}
static void operator delete(void *p, int value)
{
cout << "placement delete" << endl;
free(p);
}
static void operator delete(void *p)
{
cout << "operator delete" << endl;
::operator delete(p);
}
};
int main(void)
{
test *p1 = new(1) test;
p1->~test(); // Required to directly call the destructor because placement delete never invoke it.
test::operator delete (p1, 1);
test *p2 = new test;
delete p2;
return 0;
}
-
June 21st, 2005, 09:46 PM
#4
Re: overriding new
Yeah thanks, I was aware of that, just couldn't be bothered putting it in the example 
I guess I just have to redefine it then.
-
June 22nd, 2005, 03:13 AM
#5
Re: overriding new
Have you tried test* p = ::new test; ? (Note the scope resolution operator.)
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 22nd, 2005, 03:22 AM
#6
Re: overriding new
Just to clarify my last answer, here's section 5.3.4/9 of the standard:
If the new-expression begins with a unary :: operator, the allocation function’s name is looked up in the global scope. Otherwise, if the allocated type is a class type T or array thereof, the allocation function’s name is looked up in the scope of T. If this lookup fails to find the name, or if the allocated type is not a class type, the allocation function’s name is looked up in the global scope.
(Note that, by supplying an operator new in class scope, you are hiding the global one.)
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
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
|