Solved

why new operator can't be overloaded

Posted on 2003-11-24
2
2,800 Views
Last Modified: 2010-04-01
why new operator can't be overloaded in c++?
0
Comment
Question by:t_kaur
2 Comments
 
LVL 7

Assisted Solution

by:jconde
jconde earned 60 total points
ID: 9809255
The new operator *can* be overloaded in C++ ... There is usually no need for overloading it, but sometimes, its the easiest way to keep complete track of memory leaks.  When developing complex application where the code must be completely portable, I usually override the new and delete operator to detect memory leaks.  Its the easiest and cheapest way of knowing when you have a mem-leak or not.

here are some links which you might find useful:

http://www.flipcode.com/tutorials/tut_memleak.shtml
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarvc/html/msdn_fangle.asp
http://community.borland.com/article/0,1410,18041,00.html
http://www.bearcave.com/software/c++_mem.html
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 65 total points
ID: 9816038
In C++, new as well as delete can be overloaded.

But that does not mean that u can bypass the Constructor and destructor calls.

Normally, the new operator is responsible for the following tasks

1. Allocate memory [usually done using malloc OR equivalent]
2. Call the Constructor for the newly allocated objects

Thus, u cannot get hold of the allocated memory before the contsrtcir call.

Now, when u overload new, the only part u can replace is the 1st part. The second part is bound to happen.
Similarly with delete.

While overloading new, u have 2 options
Either overload it globally OR overload it for a class

when u overload globally, all your memory allocation new calls will go to your overloaded one
If u overload for a class, only the new calls for that class would go to the overloaded one

Your overloaded operator function should return a void *, and it should take a parameter of type size_t.

This size_t value would be supplied by the compiler

HTH

Amit



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now