// JP opened flex table

Click to See Complete Forum and Search --> : Explicitly constructing objects


roshambo
March 8th, 2008, 12:41 PM
I'm trying to explicitly construct an object with operator new. I've searched and wrote the following code.


template <class T> T *Alloc()
{
char *x = (char *)malloc(sizeof(T));
T* ret = ::new(x) T();
return (T *)x;
}


Compiling this in Visual Studio (2005) gives the following error:


error C2660: 'operator new' : function does not take 2 arguments


While I know what this error means, it seems it does work, just not on my compiler. Is there a MVS 2005 version?

roshambo
March 8th, 2008, 12:51 PM
As a side note, I cannot just use:

T* ret = new T;

Plasmator
March 8th, 2008, 01:11 PM
You probably didn't include the standard new header, did you?

roshambo
March 8th, 2008, 01:33 PM
No, thanks for that.

//JP added flex table