Solved

overloading new and delete operators ???

Posted on 2000-02-18
3
175 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
  • 2
3 Comments
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
switch public to private.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
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 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++.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now