Jump to content
  • Advertisement
Sign in to follow this  
maya18222

c++ new

This topic is 3271 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

is it possible to do something like this?
#include <iostream>
using namespace std;

namespace MyNew{
	void* operator new(size_t size){
		cout << "Allocating : "<< size << " bytes/n";
		void* p = ::operator new(size);	//call global new op
		return  p;
	}
}


int main()
{
	int* i = MyNew::operator new int();	//call my new op
	getchar(); 
}

//errors
Error	1	error C2440: 'initializing' : cannot convert from 'void *(__cdecl *)(size_t)' to 'int *'	
Error	2	error C2144: syntax error : 'int' should be preceded by ';'	


Share this post


Link to post
Share on other sites
Advertisement
A working call would look like this:


int* i = (int*)MyNew::operator new(sizeof(int)); //call my new op


But if you are willing to go to these lengths, why not so:


#include <iostream>

template <class T>
T* MyNew(const T& t = T())
{
std::cout << "Allocating : "<< sizeof(T) << " bytes\n";
return new T(t);
}

int main()
{
int* i = MyNew<int>();
}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!