Hi,
I am writing a sample code like this
#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:perator 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:perator delete (void *p){
C* pc = static_cast<C*>(p);
free(p);
}
int main() {
C *p = new C; // calls C::new
delete p; // calls C:elete
}
Here while using the new operator or inside the new operator function, i have not mentioned any type casting to type 'C', more over i am returning a void pointer inside the operator function, in this case how the compiler is ablt to match it properly ?
Is it the C++ compiler(language) technique /Is it the new operator technique ?




perator new (size_t size) throw (const char *){
elete
Reply With Quote