469,632 Members | 1,732 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

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 1261

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by | last post: by
7 posts views Thread by Jürgen Kaminski | last post: by
5 posts views Thread by Jeff Greenberg | last post: by
5 posts views Thread by Tony Johansson | last post: by
9 posts views Thread by nicolas.hilaire | last post: by
2 posts views Thread by Tom Smith | last post: by
reply views Thread by erik.erikson | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.