multiple inheritance and overloading operator new/delete

C

cppsks

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::eek:perator new(unsigned
int)
ambiguous.cc:9: static void * Base1::eek:perator 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.
 
V

Victor Bazarov

cppsks said:
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::eek:perator
new(unsigned
int)
ambiguous.cc:9: static void * Base1::eek:perator 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
 
M

Markus Elfring

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top