Jump to content
  • Advertisement
Sign in to follow this  
SecondBest

Overloading Global operator new in C++

This topic is 4100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello All I overloaded the global operator new to act in a certain manner when provided with additional arguments.
class key{};

void* operator new(size_t sz, key&)
{
    //do some stuff
    return ::operator new(sz);
};
This works fine, except for classes for which operator new is already overloaded.
class test
{
public:
	void* operator new(size_t sz)
	{
		cout << "using custom new"<<endl;
		return ::operator new(sz);
	};
};

int main(int argc, char* argv[])
{
	k k1;
	int*  p_int = new(k1) int;\\ok
        test* p_test1 = new test;\\ok
        test* p_test2 = new(k1) test;\\error
        test* p_test3 = ::new(k1) test;\\ok, but doesn't use test's operator new
	return 0;
}
I am compiling using VC++ 6 and the error I get is: error C2660: 'new' : function does not take 2 parameters Is there a way to support creating my custom new without affecting user class defined new? thanks

Share this post


Link to post
Share on other sites
Advertisement
I want my global operator new to do it's stuff and then call whatever is supplied by the system (i.e. default new, other overloaded global new operators, class specific new operator).

Share this post


Link to post
Share on other sites
I doubt there is any simple solution to this. Your operator new has no information about what type it's being called on, so any "forwarding" also cannot depend on that type.

The simplest thing you can do is implement intrusively the operator new(size_t, key&) in any classes you wish to use it on, and have it forward the call to the class' operator new.

Share this post


Link to post
Share on other sites
The purpose of a placement-new operator is to allocate memory for an object. The purpose of a class-specific new operator is to allocate memory for an object. If you were to invoke both of those, that would equate to allocating memory twice. What are you trying to accomplish.. tracking allocations?

Share this post


Link to post
Share on other sites
Just a small correction. Placement new doesn't allocate any memory but constructs an object on a previously allocated memory or even on a stack.

Share this post


Link to post
Share on other sites
Quote:
Original post by KreK
Just a small correction. Placement new doesn't allocate any memory but constructs an object on a previously allocated memory or even on a stack.

You're thinking specifically of the placement-new operator which takes a void*. Semantically, the general meaning of new, including placement-new, is the allocation of memory to a resource (note that the term "allocation" doesn't necessarily imply malloc).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!