473,396 Members | 1,929 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,396 software developers and data experts.

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(ptr:" << 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:0x3d0d0
operator Base::delete(ptr:0x3d0d0)

===================
operator Base::new(size:12)
Base:0x3d0d8
Base2:0x3d0d0
Base1:0x3d0d4
SubClass1:0x3d0d0
operator Base::delete(ptr:0x3d0d4)

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

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

Similar topics

5
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...
7
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:...
5
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...
0
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...
5
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...
9
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
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...
3
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...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.