Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

overloading new[] as a member function

Status
Not open for further replies.

StonedCoder

Programmer
Aug 6, 2001
5
GB
im trying to overload new[] for my own string class Cstr.
My code compiles on other compilers but not on msvc6.
anyone know how to get this working?

the error i get is new[] is not a member of global namespace.

I have the usual headers included and a using namespace std statement.

this is the function that errors.....

and by the way get same error for operator delete[]

Code:
void* Cstr::operator new [] (size_t size) throw(std::bad_alloc)

{

	void* memory;
	try
	{
		memory=::operator new [] (size); // use global new to allocate memory
	}
	catch(std::bad_alloc&)
	{
		cerr<<endl<<&quot;Out of memory.... aborting....&quot;<<endl;
		abort();
	}

	return memory; // if no exception thrown return pointer to allocated memory
}
 
see a sample. Don't call new inside operator, it will done infinite recursion.
#include<stdlib.h>
class sxx
{
void* buffer;
public:
sxx(){}
void* operator new[](size_t x)
{
cout<<&quot;called new[]&quot;<<endl;
return malloc(x*sizeof(sxx));
}
};
void main()
{
sxx* cxcx = new sxx[10];
} John Fill
1c.bmp


ivfmd@mail.md
 
no i disagree....

i am overloading new as a member function passing on the call to global new[]

i have seen similar code in books and examples and it compiles on gnu g++ or whatever it is but wont compile on MSVC. i think this problem is msvc specific.

doesn't cause infinite recursion on other compilers.
 
yes, sure it is. Try to #include<memory> and use std::new or #include<memory.h> John Fill
1c.bmp


ivfmd@mail.md
 
have already tried that and memory is included anyway..

have tried making it static but it is implicitly staic anyway.

any other ideas as to why global new has gone missing.

Item 9 in scott meyers effective c++ mentions if you use a form of the operator with a different number of arguements to normal you hide the global new[] but i am using the same number of arguements as global new[] and it compiles on other compilers....
 
in C++ it makes no sence, using a default operator new inside an overloaded operator. There it nakes sence for example when you use WinAPI functions, or COM functions, or some specific, not default operator. John Fill
1c.bmp


ivfmd@mail.md
 
I mean there VisualC++, not usual C++. John Fill
1c.bmp


ivfmd@mail.md
 
I guess im just being lazy but rather than put every use of new[] in a try/catch block i thought that i would overload the operator to do it for me.

This is just an exercise in getting it right.I have never before had cause to overload new[] and i know i could have used set_new_handler() instead but i wanted to try it this way after reading effective c++ and thinking in c++.

anyhow I seem to have it working now so thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top