Solved

why new operator can't be overloaded

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

752 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