?
Solved

overloading new and delete operators ???

Posted on 2000-02-18
3
Medium Priority
?
186 Views
Last Modified: 2010-05-19
How do you handle overloading new and delete operators(including their
array equivalents) in inheritance.ie(the base class alone holds the overloaded
functions and derived class has none)
0
Comment
Question by:rajesh_ganapathy
[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
3 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2537849
switch public to private.
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2537997
Wyn, what does that even mean?

rajesh, there is nothing "special" to be done for this, except perhaps to consider the fact that the new/delete operators may be allocating objects of different sizes (since derived objects may have more data members than the base class objects)  However, this isn't much of an isssue as the size is always passed to new and new[].  (The size is not passed to delete, but that usually isn't a problem (if it is, the size can usually be recorded in memory and somewhere, it depends on what you are doing)

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2538001
wyn, I got what you mean now.  I doubt that is his problem.  These functions need to be public (unless you intend to use them only from withing the class).


void * ClassName::operator new(size_t  Siz)
{
   return malloc(Siz);
}

void * ClassName::operator new [](size_t  Siz)                    
{
   return malloc(Siz);
}

void ClassName::operator delete(void *MemPtr)
{
   free(MemPtr);
}

void ClassName::operator delete [](void *MemPtr)
{
   free(MemPtr);
}


Let me know if you have questions.
0

Featured Post

Technology Partners: 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

801 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