Solved

overloading new and delete operators ???

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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. …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

930 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

13 Experts available now in Live!

Get 1:1 Help Now