By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

inheriting new and delete

P: n/a
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 ::operator new(sz);
}
void operator delete(void* v,size_t sz)
{
cout<<"delete sz="<<sz<<endl;
::operator 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.

Jun 13 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
dragoncoder wrote:
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 ::operator new(sz);
}
void operator delete(void* v,size_t sz)
{
cout<<"delete sz="<<sz<<endl;
::operator 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '07 #2

P: n/a
On 13 Jun, 16:37, dragoncoder <pktiw...@gmail.comwrote:
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
Jun 13 '07 #3

P: n/a
On Jun 13, 11:58 am, dasjotre <dasjo...@googlemail.comwrote:
On 13 Jun, 16:37, dragoncoder <pktiw...@gmail.comwrote:
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
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.

Jun 13 '07 #4

P: n/a
On 13 Jun, 17:01, dragoncoder <pktiw...@gmail.comwrote:
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

Jun 13 '07 #5

P: n/a
On 13 Jun, 17:41, dasjotre <dasjo...@googlemail.comwrote:
On 13 Jun, 17:01, dragoncoder <pktiw...@gmail.comwrote:
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
sorry that would be 5.3.5 instead.

DS

Jun 14 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.