469,632 Members | 1,639 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.

multiple inheritance and overloading operator new/delete

Here is the code that I am working with:

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

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

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

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";
new SubClass; // line 39
}

// Here is the output from the compiler:
ambiguous.cc: In function `int main()':
ambiguous.cc:39: request for member `operator new' is ambiguous
ambiguous.cc:17: candidates are: static void * Base2::operator new(unsigned
int)
ambiguous.cc:9: static void * Base1::operator new(unsigned
int)
ambiguous.cc:39: confused by earlier errors, bailing out
What options do I have in order to resolve this conflict? Please assume that
removing multiple inheritance is not an option.

Thanks.


Jul 22 '05 #1
2 4233
"cppsks" <sk*****@hotmail.com> wrote...
Here is the code that I am working with:

#include <new>
#include <iostream>
#include "stdlib.h"
using std::cout; using std::endl;

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

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

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";
new SubClass; // line 39
}

// Here is the output from the compiler:
ambiguous.cc: In function `int main()':
ambiguous.cc:39: request for member `operator new' is ambiguous
ambiguous.cc:17: candidates are: static void * Base2::operator
new(unsigned
int)
ambiguous.cc:9: static void * Base1::operator new(unsigned
int)
ambiguous.cc:39: confused by earlier errors, bailing out
What options do I have in order to resolve this conflict? Please assume
that
removing multiple inheritance is not an option.


I don't think you have many options, to be honest. You will need to
overload the 'new' and 'delete' for the SubClass as well.

V
Jul 22 '05 #2
> int main()
{
cout << "\n===================\n";
new SubClass; // line 39
}
[...]
ambiguous.cc:39: confused by earlier errors, bailing out


It is also interesting that this example works to produce the error
message so that the compilation is aborted.
I would normally expect an assignment to a function argument or
variable of the pointer from the new operator as the next step.

Regards,
Markus
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ian McBride | last post: by
3 posts views Thread by Nimmi Srivastav | last post: by
5 posts views Thread by | last post: by
3 posts views Thread by silver360 | 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.