Hello
When compiling the following example
[CODE=cpp]#include <cstdlib> //declarations of malloc and free
#include <new>
#include <iostream>
using namespace std;
class C {
public:
C();
void* operator new (size_t size); //implicitly declared as a static member function
void operator delete (void *p); //implicitly declared as a static member function
};
void* C::operator new (size_t size) throw (const char *){
void * p = malloc(size);
if (p == 0) throw "allocation failure"; //instead of std::bad_alloc
return p;
}
void C::operator delete (void *p){
C* pc = static_cast<C*> (p);
free(p);
}
int main() {
C *p = new C; // calls C::new
delete p; // calls C::delete
}[/CODE]
It something wrong with new operator overloading ?
What's this mean
... operator new(size_t) throw (const char*)' throws different exceptions
When compiling the following example
[CODE=cpp]#include <cstdlib> //declarations of malloc and free
#include <new>
#include <iostream>
using namespace std;
class C {
public:
C();
void* operator new (size_t size); //implicitly declared as a static member function
void operator delete (void *p); //implicitly declared as a static member function
};
void* C::operator new (size_t size) throw (const char *){
void * p = malloc(size);
if (p == 0) throw "allocation failure"; //instead of std::bad_alloc
return p;
}
void C::operator delete (void *p){
C* pc = static_cast<C*> (p);
free(p);
}
int main() {
C *p = new C; // calls C::new
delete p; // calls C::delete
}[/CODE]
Code:
g++ -Wall example.cpp -o example example.cpp:14: error: declaration of 'static void* C::operator new(size_t) throw (const char*)' throws different exceptions example.cpp:9: error: from previous declaration 'static void* C::operator new(size_t)' example.cpp: In static member function 'static void C::operator delete(void*)': example.cpp:21: warning: unused variable 'pc'
It something wrong with new operator overloading ?
What's this mean
... operator new(size_t) throw (const char*)' throws different exceptions
Comment