S
Stanley Rice
The global new operator function has three overloaded prototype, and one of it
is:
void * operator new(size_t s, void *p) throw();
The document says that the above function doesn't allocate memory, instead, it
just call the constructor of the given type and place the address of the
object on the given allocated memory pointed by p. and it is "equal to"
new (type) p;
I tried it, but failed. I have the following code snippet:
**********************************************************************
struct myclass {
myclass() { cout << "constructor" << endl; }
};
myclass *p = operator new(sizeof *p)); // just allocate memory
new (p) myclass; // works, the constructor is called.
// operator new(sizeof *p, p); // doesn't work, no message printed.
***********************************************************************
What's going on there? Am I misunderstand something?
Thanks in advance.
is:
void * operator new(size_t s, void *p) throw();
The document says that the above function doesn't allocate memory, instead, it
just call the constructor of the given type and place the address of the
object on the given allocated memory pointed by p. and it is "equal to"
new (type) p;
I tried it, but failed. I have the following code snippet:
**********************************************************************
struct myclass {
myclass() { cout << "constructor" << endl; }
};
myclass *p = operator new(sizeof *p)); // just allocate memory
new (p) myclass; // works, the constructor is called.
// operator new(sizeof *p, p); // doesn't work, no message printed.
***********************************************************************
What's going on there? Am I misunderstand something?
Thanks in advance.