473,383 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Overloading New Operator

Min
class Foo
{
public:
char name[20];
Foo() { /*** Do Something ***/ }
Foo( char* str) { /*** Do Something ***/ }

static void * operator new(unsigned int size)
{
//*********************
// Code from Effective C++
//*********************
void* memory = null;
if ( size == 0 ) { size = 1; }
try
{
memory = ::operator new(size);
}
catch (std::bad_alloc&)
{
throw;
}
return memory;
}
};

void main()
{
Foo * ptrFoo1 = new Foo(); // Uses Overloaded New
Foo * ptrFoo2 = new Foo("What's up"); // Uses Overloaded New
Foo *ptrFoo3 = new Foo[10]; // Uses DEFAULT New.
}

//*******************************************
Using a debugger (VC++ 6.0 if it is important), I find that ptrFoo3 does not
use the overloaded new operator. Anybody knows why ? or if I am missing
something. Or is it the vendor ? or language ?
Jul 19 '05 #1
1 13475

"Min" <no****@home.com> wrote in message news:Jy**********************@news1.calgary.shaw.c a...
void main()
main returns int.
{
Foo * ptrFoo1 = new Foo(); // Uses Overloaded New
Foo * ptrFoo2 = new Foo("What's up"); // Uses Overloaded New
Foo *ptrFoo3 = new Foo[10]; // Uses DEFAULT New. Using a debugger (VC++ 6.0 if it is important), I find that ptrFoo3 does not
use the overloaded new operator. Anybody knows why ? or if I am missing
something. Or is it the vendor ? or language ?

You're missing something. Arrays use the operator new[] allocation function
(which you haven't overloaded).

I'll take your word for the fact you cobbed that allocator form a book, but it's
overly silly:
1. the size_t arg will never be zero (unless some clown explicitly calls it that way)
and it's not clear why you want to bump it up in that case anyhow.
2. The catch block that does nothing other than rethrow is a bit silly.
3. You could just
return ::operator new(size);
right from the try block.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.