473,411 Members | 2,185 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,411 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 4553
"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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.