inheriting new and delete

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 ::eek:perator new(sz);
}
void operator delete(void* v,size_t sz)
{
cout<<"delete sz="<<sz<<endl;
::eek:perator delete(v);
}
};

class Derived:public 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.
 
V

Victor Bazarov

dragoncoder said:
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 ::eek:perator new(sz);
}
void operator delete(void* v,size_t sz)
{
cout<<"delete sz="<<sz<<endl;
::eek:perator delete(v);
}
};

class Derived:public 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.

Sure. You don't need an instance of the class for them to be invoked.
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.

Huh? Why not? 8-O
But in the above code, for both
Base and Derived, the overloaded functions are being called. Can
someone please explain why ?

Nothing in the Standard says that static members are *not* inherited.

V
 
D

dasjotre

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 ?

because of lookup rules for finding members of a class
and its base classes.

Because standard says so ;)

DS
 
D

dragoncoder

because of lookup rules for finding members of a class
and its base classes.

Because standard says so ;)

DS

Thanks everyone for the reply. Haven't had my coffee today morning.
Anyways, I have one more question is the signature of delete void
operator delete(void* v,size_t sz) a valid one? Because I could not
find this in the standard. Thanks again.
 
D

dasjotre

Thanks everyone for the reply. Haven't had my coffee today morning.
Anyways, I have one more question is the signature of delete void
operator delete(void* v,size_t sz) a valid one? Because I could not
find this in the standard. Thanks again.

the second argument is not necessary, if present
compiler will put the number of bytes to delete.
It is useful if you're writing your allocator.

5.3.4 Delete
 
D

dasjotre

the second argument is not necessary, if present
compiler will put the number of bytes to delete.
It is useful if you're writing your allocator.

5.3.4 Delete

sorry that would be 5.3.5 instead.

DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top