Solved

overloading new and delete operators ???

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Move constructor only called if marked noexcept? 6 120
C++ question 3 81
VS2015 Redefinition errors 4 101
How do i run a c++ file? 15 57
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 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