473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

inheriting new and delete

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
5 1336
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do...
26
by: BCC | last post by:
Hi, A colleague has some code like this: class CMyObject { // Bunch of Member functions } class CMyObjectList: public std::vector<CMyObject> {
29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
2
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that...
1
by: Ryan Shaw | last post by:
I’m having trouble with Typed Dataset I would like to add functionality to my typed dataset at the business layer such as delete rules or editing rules by inheriting it Can I inherit the...
3
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
1
by: Peter J. Bismuti | last post by:
How do you access attributes of a class when inheriting from it? Can't you just say: self.attribute? Help?!...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
17
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.