473,779 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(j j){}
};

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 1354
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(j j){}
};

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...@googl email.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...@googl email.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
12057
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 this. I have understood that there is a relation between streambuf and iostream, where one should be able to extend streambuf and override the underflow/overflow functions (and do send()/recv() for example).
26
10738
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
5904
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 bitset<64>, but I know that STL has no virtual destructor. Can I get around this by calling the baseclass destructor explicitly in my derived class? Is there another way to get all of the bitset<64> functionality without rewriting a lot of...
2
1813
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 this is the right approach (please tell me if it is not), how then do I make it so that the group box cannot be moved around on my set of controls, but is also able to act as container for other controls? If I leave the modifier of the group box...
1
1119
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 datatable? I’m having trouble because of the constructor Can I override the row change event routines? How do I do that How can I control these features in a typed dataset without my presentation layer having to do it
3
5385
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 bar ( attribute integer NOT NULL ) INHERITS (foo);
1
1593
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?! ........................................................................................................ #!/usr/bin/python from Dialog import Dialog
24
2962
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 vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
17
2449
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 and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as the class object? Yes, I am aware that C++ does not do this. I just want to know everyones...
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.