Solved

overloading new and delete operators ???

Posted on 2000-02-18
3
184 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 50 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

690 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