S
skscpp
I have a question about multiple inheritance, operator new/delete and
ambiguity.
Here is some code that I wrote to analyze this problem.
// test.h
==========================
#include <new>
#include <iostream>
#include "stdlib.h"
class Base1
{
public:
Base1() { cout << "Base1:" << this << endl; }
int hi;
};
class Base2
{
public:
Base2() { cout << "Base2:" << this << endl; }
void* operator new(size_t size) throw (std::bad_alloc) { cout << "operator
new\n"; return malloc(size); }
void operator delete(void* ptr) throw() { cout << "operator delete\n";
free(ptr); }
};
class SubClass : public Base1, public Base2
{
public:
SubClass() { cout << "SubClass:" << this << endl; }
};
// test.cc
====================
#include "test.h"
int main()
{
cout << "\n===================\n";
Base1* b1 = new Base1();
delete b1;
cout << "\n===================\n";
Base2* b2 = new Base2();
delete b2;
cout << "\n===================\n";
SubClass* sc = new SubClass();
delete sc;
cout << "\n===================\n";
}
// result
====================
===================
Base1:0x3cc78
===================
operator new
Base2:0x3cc78
operator delete
===================
operator new
Base1:0x3cc78
Base2:0x3cc7c
SubClass:0x3cc78
operator delete
===================
This was done with gcc version 2.95.3 20010315 (release)
Is that the correct behavior?
Shouldn't it complain and state that "new" or "delete" on SubClass is
ambiguous?
Thanks.
ambiguity.
Here is some code that I wrote to analyze this problem.
// test.h
==========================
#include <new>
#include <iostream>
#include "stdlib.h"
class Base1
{
public:
Base1() { cout << "Base1:" << this << endl; }
int hi;
};
class Base2
{
public:
Base2() { cout << "Base2:" << this << endl; }
void* operator new(size_t size) throw (std::bad_alloc) { cout << "operator
new\n"; return malloc(size); }
void operator delete(void* ptr) throw() { cout << "operator delete\n";
free(ptr); }
};
class SubClass : public Base1, public Base2
{
public:
SubClass() { cout << "SubClass:" << this << endl; }
};
// test.cc
====================
#include "test.h"
int main()
{
cout << "\n===================\n";
Base1* b1 = new Base1();
delete b1;
cout << "\n===================\n";
Base2* b2 = new Base2();
delete b2;
cout << "\n===================\n";
SubClass* sc = new SubClass();
delete sc;
cout << "\n===================\n";
}
// result
====================
===================
Base1:0x3cc78
===================
operator new
Base2:0x3cc78
operator delete
===================
operator new
Base1:0x3cc78
Base2:0x3cc7c
SubClass:0x3cc78
operator delete
===================
This was done with gcc version 2.95.3 20010315 (release)
Is that the correct behavior?
Shouldn't it complain and state that "new" or "delete" on SubClass is
ambiguous?
Thanks.