D
dragoncoder
I got this code from a friend of mine.
#include <iostream>
using namespace std;
class Base
{
int i;
public:
Base(int ii=0):i(ii){}
void * operator new(size_t sz)
{
cout<<"new sz="<<sz<<endl;
return :perator new(sz);
}
void operator delete(void* v,size_t sz)
{
cout<<"delete sz="<<sz<<endl;
:perator delete(v);
}
};
class Derivedublic Base
{
int j;
public:
Derived(int ii=0,int jj = 0):Base(ii),j(jj){}
};
int main(int argc, char *argv[])
{
Derived *d = new Derived;
delete d;
Base *b = new Base;
delete b;
return 0;
}
I have a question here. operator new and delete functions are by
definition static to a class. So, if I have defined my own version of
operator new() or operator delete() in a base class, a derived class
should not inherit that from the base. But in the above code, for both
Base and Derived, the overloaded functions are being called. Can
someone please explain why ?
Thanks in advance.
#include <iostream>
using namespace std;
class Base
{
int i;
public:
Base(int ii=0):i(ii){}
void * operator new(size_t sz)
{
cout<<"new sz="<<sz<<endl;
return :perator new(sz);
}
void operator delete(void* v,size_t sz)
{
cout<<"delete sz="<<sz<<endl;
:perator delete(v);
}
};
class Derivedublic Base
{
int j;
public:
Derived(int ii=0,int jj = 0):Base(ii),j(jj){}
};
int main(int argc, char *argv[])
{
Derived *d = new Derived;
delete d;
Base *b = new Base;
delete b;
return 0;
}
I have a question here. operator new and delete functions are by
definition static to a class. So, if I have defined my own version of
operator new() or operator delete() in a base class, a derived class
should not inherit that from the base. But in the above code, for both
Base and Derived, the overloaded functions are being called. Can
someone please explain why ?
Thanks in advance.