473,581 Members | 2,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mul. inheritance & overloading operator new/delete solved by virtual base inheritance?

Hello. I posted a question regarding this yesterday. I came up with the
following solution but I am a little hesistant as to this solution having
any side-effects that I am not aware of. The solution seems to work though.
Please let me know if this is okay or if I need to modify something here.
Thanks !!!!

Here is what I have:

#include <new>
#include <iostream>
#include "stdlib.h"

class Base
{
public:
Base() { cout << "Base:" << this << endl; }
void* operator new(size_t size) throw (std::bad_alloc ) { cout << "operator
Base::new(size: " << size << ")\n"; return malloc(size); }
void operator delete(void* ptr) throw() { cout << "operator
Base::delete(pt r:" << ptr << ")\n"; free(ptr); }
};

class Base1 : public virtual Base
{
public:
Base1() { cout << "Base1:" << this << endl; }
};

class Base2 : public virtual Base
{
public:
Base2() { cout << "Base2:" << this << endl; }
};

class SubClass : public Base1, public Base2
{
public:
SubClass() { cout << "SubClass:" << this << endl; }
};

class SubClass1 : public Base2, public Base1
{
public:
SubClass1() { cout << "SubClass1: " << this << endl; }
};
int main()
{
cout << "\n============ =======\n";
Base1* hi = new SubClass;
delete hi;
cout << "\n============ =======\n";
hi = new SubClass1;
delete hi;
cout << "\n============ =======\n";
}
Here is the output:

=============== ====
operator Base::new(size: 12)
Base:0x3d0d8
Base1:0x3d0d0
Base2:0x3d0d4
SubClass:0x3d0d 0
operator Base::delete(pt r:0x3d0d0)

=============== ====
operator Base::new(size: 12)
Base:0x3d0d8
Base2:0x3d0d0
Base1:0x3d0d4
SubClass1:0x3d0 d0
operator Base::delete(pt r:0x3d0d4)

=============== ====
Jul 22 '05 #1
0 1429

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5229
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched...
7
2048
by: Jürgen Kaminski | last post by:
Hello all, I got some legacy code where a template implements a non-templated interface. These classes are really HUGE, but it boils down to the following structure: class BaseClass { public: virtual ~BaseClass() {} virtual bool Create(size_t size) = 0;
5
2110
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I can get! Consider 3 classes in the following heirarchy: base / \ deriv1 deriv2 \
0
505
by: cppsks | last post by:
Hello. I posted a question regarding this yesterday. I came up with the following solution but I am a little hesistant as to this solution having any side-effects that I am not aware of. The solution seems to work though. Please let me know if this is okay or if I need to modify something here. Thanks !!!! Here is what I have: #include...
5
2076
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program works fine but there in one thing that I'm unsure about and that is the inheritance statement. What difference is it if I have this construction...
9
372
by: nicolas.hilaire | last post by:
Hi all, I have somes classes that have the same concept. I would like to instance one of them depending on a condition Something like this MyObj myObj; if (i==0)
2
7388
by: Tom Smith | last post by:
I'm having difficulty with overloading ==, and it's making my brain melt - can you help...? What I want to have is: 1) A base class A with virtual operator== defined (no problem) 2) A class B derived from A with operator== overridden (again, no problem) 3) Pointers A* b1, b2 which may actually point to instances of B, where I can call...
3
2118
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think meets these requirements, but after reading the chapter on exceptions in 'Exceptional C++':Sutter, I am not sure if its is really Exception safe or...
0
2043
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the increment operator in a class and then defining a derived class and using the derived class. When I try to use the increment operator on an instance of...
0
7882
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...
0
8157
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. ...
1
7914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6564
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5683
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...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
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...
1
1410
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.