Saturday, March 15, 2008

Overloading the NEW operator - Part II

The NEW operator has, among many advantages, then ability to be overloaded class-wise. In other words, you can overload the NEW operator on a per-class basis, so that your new operator is specifically tailored to respond to what type of object you are applying the NEW operator to.

So you can overload the NEW operator for each class to perform in special situations like handling the case when memory runs out and so on..

Sample code:

class myClass
{
char abc;
int def;
public:
void *operator new(size_t bytes)
{
.....specific code.......
}

};
class otherClass
{
char a;
int b;
public:
void *operator new(size_t bytes)
{
.....specific code.......
}

};
void main()
{
myClass obj1= new myClass();
otherClass obj2 = new otherClass();
.......................
.......................
}

7 comments:

QuackWare said...

Cool Stuff, can you check out my C++ Code Samples too?

Martin said...

oh my god. do you even realize that's the whole reason in overloading operators? to make them behave diffferently when dealing with a specific class/object?

it's the same all over your blog. you are stating the obvious...

Ronny said...

gd one...check out for more details. http://learnsthgeveryday.blogspot.com/

Creature care said...

but how these all done............???

Nabil ur Rehman said...

http://softwarepings.blogspot.com/

For beginners visit above link.

manish gupta said...

thanx for the information..more more details visit www.studentluv.com

manish gupta said...
This comment has been removed by the author.