473,414 Members | 1,599 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,414 software developers and data experts.

operator new in custom namespace

Why doesn't the standard allow to overload operator new/delete in my
namespace, e.g.

namespace Foo
{
void* operator new( std::size_t sz );
void operator delete( void* address );
}

On the other hand I can overload new/delete per class:

class Foo1
{

static void* operator new( std::size_t sz );
static void operator delete( void* address );

};

And finally what happens if I have classes A and B that overwrite new
and delete and a class C that now inherits from both. What is the
expected behaviour there?

Cheers,
-Dirk

Apr 11 '07 #1
2 3116
di**@dirkgregorius.de wrote:
Why doesn't the standard allow to overload operator new/delete in my
namespace, e.g.

namespace Foo
{
void* operator new( std::size_t sz );
void operator delete( void* address );
}

On the other hand I can overload new/delete per class:

class Foo1
{

static void* operator new( std::size_t sz );
static void operator delete( void* address );
'static' is implicit here, IIUIC. IOW, you don't need to use the
keyword in the function declaration.
>
};

And finally what happens if I have classes A and B that overwrite new
and delete and a class C that now inherits from both. What is the
expected behaviour there?
Not sure what having operator new in its own namespace would do. The
language says that if a class has operator new overloaded, then the
operator is used to allocate memory when constructing a dynamic object
of that class (chosen over the global operator new). What are you
trying to accomplish with the overloaded operator new in a namespace?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 11 '07 #2
On Apr 11, 11:04 am, d...@dirkgregorius.de wrote:
Why doesn't the standard allow to overload operator new/delete in my
namespace, e.g.
namespace Foo
{
void* operator new( std::size_t sz );
void operator delete( void* address );
}
Historical reasons, I would imagine.
On the other hand I can overload new/delete per class:
class Foo1
{
static void* operator new( std::size_t sz );
static void operator delete( void* address );
};
And finally what happens if I have classes A and B that overwrite new
and delete and a class C that now inherits from both. What is the
expected behaviour there?
Ambiguous. The compiler should complain.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steven Brent | last post by:
Dear Group: I am a little perplexed by a basic example from the O'Reilly book, 2nd ed., which demonstrates overloading some builtin list operators by embedding lists in a wrapper class. Below...
2
by: Xavier Decoret | last post by:
The following code does not compoile with gcc-3.2.3 namespace dummy { //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Interface of Foo...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
8
by: Jason C | last post by:
Hi, I am coming from C background and trying to learn C++. So bear with me if the answer to my question is obvious. I am trying to overload the "+" operator in different namespaces, like this:...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
2
by: Dennis Zickefoose | last post by:
The following code yields error CS0131: "The left-hand side of an assignment must be a variable, property or indexer": (new int())++; However, when using a class with a custom operator++, an...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
5
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.