operator new/delete

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.
 
O

Oplec

skscpp said:
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.

From what I understand, there should not be an ambiguity since Base1 is
using the global new/delete, Base2 is using its classes new/delete, and
Subclass is using the new/delete it inherits from Base2. Since you are
making your own new/delete, you really should be declaring your
destructors to be virtual so that delete works properly.

Hope that helps
 
R

Ron Natalie

skscpp said:
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?
What's ambiguous about it? There is no "implicit" operator new defined
for classes (like a copy constructor etc...). It is only when the look up
fails to find the method in the scope of the allocated object that it resorts
to looking at the global one.

In this case operator new is looked up in the scope of SubClass which has
an unambiguous resolution in Base2::eek:perator new.

5.3.4/9 defines this.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top