I am trying to overload operator new using templates, but the overloaded function is not getting called. I am very new to templates, can some one please point me to what I am missing.
#include<iostream>
using namespace std;
template <typename T>
void* operator new(size_t size)
{
void *p = malloc(size);
cout <<"I am in template";
return p;
}
template <typename T>
void* operator new[] (size_t size)
{
void *p = malloc(size);
cout <<"I am in template";
return p;
}
int main()
{
int *a;
int b;
a = new int[10];
cin >> b;
return 0;
}
Changed and working code is as below(I followed the link mentioned in comment):
void * operator new(std::size_t n) throw(std::bad_alloc)
{
void *p = malloc(n);
cout <<"I am in template";
return p;
}
void operator delete(void * p) throw()
{
//...
}
void *operator new[](std::size_t s) throw(std::bad_alloc)
{
void *p = malloc(s);
cout <<"I am in template";
return p;
}
void operator delete[](void *p) throw()
{
// TODO: implement
}
int main()
{
int *a;
int b;
a = new int[10];
cin >> b;
return 0;
}