placement new體驗

#include <iostream>
using namespace std;
class A
{
public:
 A();
 A(const A&);
 int i;
};
A::A()
{
 i = 0;
 cout << "default"<<endl;
}
A::A(const A&)
{
 cout << "copy"<<endl;
}
void main()
{
 A a;
 a.i = 1;
 A b;
 b.i = 2;
 A* p = &a;

 new(p) A;
 new(p) A(b);
 cout<< a.i<<endl;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章