Solved

why new operator can't be overloaded

Posted on 2003-11-24
2
2,846 Views
Last Modified: 2010-04-01
why new operator can't be overloaded in c++?
0
Comment
Question by:t_kaur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

624 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