Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

why new operator can't be overloaded

Posted on 2003-11-24
2
Medium Priority
?
2,862 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 240 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 260 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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…
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.

705 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