Solved

why new operator can't be overloaded

Posted on 2003-11-24
2
2,789 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

11 Experts available now in Live!

Get 1:1 Help Now