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

operator new override/overload?

Hi, if we need to change the behavior of operator new, it is called
overriding or overloading? My other question is, if we change the
behavior of operator new, do we use malloc to do that or we use
operator new?

Jan 13 '06 #1
2 11490
* Shark:
Hi, if we need to change the behavior of operator new, it is called
overriding or overloading?
operator new is a static function, and on that account it would be

However, operator delete is also a static function, but behaves much like a
virtual function with dynamic (run-time) dispatch:
#include <iostream>
#include <ostream>
#include <memory>

struct Base { virtual ~Base() {} };

struct DerivedA: Base
static void operator delete( void* p )
std::cout << "Deallocating A." << std::endl;
::operator delete( p );

struct DerivedB: Base
static void operator delete( void* p )
std::cout << "Deallocating B." << std::endl;
::operator delete( p );

int main()
std::auto_ptr<Base> pA( new DerivedA );
std::auto_ptr<Base> pB( new DerivedB );

which yields the output

Deallocating B.
Deallocating A.

as if operator delete were a virtual function.

So in the case of operator delete it's not entirely inappropriate to talk
about 'override'.

What about operator new then, viewed as a kind of same sort of function as
operator delete?

Well, it isn't. Operator new is more like a constructor (you don't override
constructors, and you don't override operator new), and operator delete is
more like a virtual destrutor (you do override virtual destructors).

My other question is, if we change the
behavior of operator new, do we use malloc to do that or we use
operator new?

You can use anything you want, it depends on what you want. But given that
you ask, i.e. not sure, my advice would be to _not_ overload operator new.
Except as a learning experiment -- don't use features you don't understand
in production code, especially not dangerous, low-level features.
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 13 '06 #2
"Shark" <cp*******@yahoo.com> wrote in message news:11**********************@g44g2000cwa.googlegr oups.com...
Hi, if we need to change the behavior of operator new, it is called
overriding or overloading? My other question is, if we change the
behavior of operator new, do we use malloc to do that or we use
operator new?

It is not clear what you want. C++ has several operators new. Which one do you need to change?
C++ has the possibility to define a new operator for a class. This is the safest way to go.
It is also possible to override the global new operators, but this needs a lot of care,
since these global new operators are also used in the C++ library functions.
(This means that on many platforms the standard operator new sits already in the shareable libraries,
so you should use a static link to get a consistent use of the changed new operators.)
In addition you should change the delete operators as well to match your new operators.
You could use malloc and free.

Jan 13 '06 #3

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

Similar topics

by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
by: apex | last post by:
Hi All : How to override the class operator = like C++ ? Best regards !
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
by: Pesso | last post by:
I'm having a difficulty compairing null to a class object whose "equal" operator is overridden.. Consider the following: class Foo { // ... public static bool operator==(Foo f1, Foo f2) {...
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?
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...

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.