473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 4541
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
0
by: cppsks | last post by:
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...
0
by: cppsks | last post by:
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...
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
3
by: Lighter | last post by:
The C++ Standard Doesn't Permit Overloading new and delete? In the 13.5 of The C++ standard (ISO/IEC 14882, 1998), I cannot find the specification on overloading the operators new and delete;...
2
by: kapilkumawat | last post by:
I have an requirement to overload the delete operator in C++, but it should also accept the sizeof() the object that is to be deleted. Actually I am trying to built a custom memory allocator and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.