Item 35. Placement New
It seems that it is impossible to find a solution in C . As, it is impossible to call the constructor directly, but you can call the constructor with the Placement New fraudor compiler.
-------------------------------------------------- 1, what is Placement New? Placement New is a standard, global version of the Operator New, which cannot be replaced by a custom version (unlike normal version of Operator New and Operator Delete can be replaced).
Void * Operator new (size_t, void * p) throw () {returnit p;}
The execution of the Placement New ignores the size_t parameter and only returns the second parameter. The result is that the user is allowed to put an object in a particular place to achieve the effect of calling the constructor. Class Sport {...}; // represents a serial portconst int coc = 0x00400000; // location of a port // ... void * comaddr = reinterpret_cast
2, NEW, OPERATOR New, and Placement New? NEW: It cannot be overloaded, and its behavior is always consistent. It first calls the operator new allocation of the memory, then call the constructor to initialize the memory. Operator New: To achieve different memory allocation behavior, you should overload Operator New, not New. DELETE and Operator delete are similar. Placement New: just a version of the Operator New overload. It does not assign memory, just returns a pointer to a certain number of memory already assigned. Therefore, it cannot be deleted, but you need to call the destructor of the object.
3. Allocate array const Int numcoms = 4; // ... sport * comports = NUMCOMS; // ... [--I] ~ SPORT ();