472,374 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 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 1368

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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.