b***y 发帖数: 2799 | 1 class A
{
};
void main(void)
{
.... allocate memory m.
A *a = new (m) A;
...
}
结果编译通不过,说NEW不接受两个参数。难道PLACEMENT NEW不象operater new那样有
个global版本? |
t****t 发帖数: 6806 | 2 placement new is declared in
【在 b***y 的大作中提到】 : class A : { : }; : void main(void) : { : .... allocate memory m. : A *a = new (m) A; : ... : } : 结果编译通不过,说NEW不接受两个参数。难道PLACEMENT NEW不象operater new那样有
|
E*****7 发帖数: 128 | 3 Assuming you have done somewhere in your program:
(1) A* m = new A;
Then try to plamcement new it as:
(2) A* a = (A*) operator new(sizeof(A), m);
Do not forget that now both a and m are pointing to the same object. (2) did not call ctor of A, i.e., (1) differs from (2) in that it calls the ctor of A. |
t****t 发帖数: 6806 | 4 your (2) is not placement new. it's the alternative form of operator new,
which placement new calls, and does nothing except returning the 2nd
parameter.
placement new, on the other hand, does exactly the opposite: calls the ctor
of A, but does not allocate memory.
did not call ctor of A, i.e., (1) differs from (2) in that it calls the ctor
of A.
【在 E*****7 的大作中提到】 : Assuming you have done somewhere in your program: : (1) A* m = new A; : Then try to plamcement new it as: : (2) A* a = (A*) operator new(sizeof(A), m); : Do not forget that now both a and m are pointing to the same object. (2) did not call ctor of A, i.e., (1) differs from (2) in that it calls the ctor of A.
|
E*****7 发帖数: 128 | 5 Can thrust give the placement new answer? Thanks. |
t****t 发帖数: 6806 | 6 the op's placement new is already correct.
【在 E*****7 的大作中提到】 : Can thrust give the placement new answer? Thanks.
|
E*****7 发帖数: 128 | 7 Do the following three steps do what placement new is supposed to do? |
t****t 发帖数: 6806 | 8 (3) call (2).
previously
【在 E*****7 的大作中提到】 : Do the following three steps do what placement new is supposed to do?
|