Skip to main content
GameDev.net gamedev.net
🔒 Locked

c++ new

Started by maya18222 Jun 9, 2009 at 3:57 PM 0 replies 850+ views
Original Post
maya18222
maya18222
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 ';'	


visitor
visitor
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>();}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.