- 论坛徽章:
- 0
|
本帖最后由 chllcy88 于 2014-11-19 14:04 编辑
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int nop() {
- static int nop_x; return ++nop_x; // Don't remove me, compiler!
- };
- class A
- {
- public:
- unsigned long long content_A;
- A(void) : content_A(0xAAAAAAAAAAAAAAAAull)
- { cout << "++ A has been constructed" << endl;};
- ~A(void)
- { cout << "-- A has been destructed" << endl;};
- void function(void) { nop(); };
- };
- void PrintMemory(const unsigned char memory[],
- const char label[] = "contents")
- {
- cout << "Memory " << label << ": " << endl;
- for (size_t i = 0; i < 4; i++)
- {
- for (size_t j = 0; j < 8; j++)
- cout << setw(2) << setfill('0') << uppercase << hex
- << static_cast<int> (memory[i * 8 + j]) << " ";
- cout << endl;
- }
- }
- int main()
- {
- unsigned char memory[32];
- memset(memory, 0x11, 32 * sizeof(unsigned char));
- PrintMemory(memory, "before placement new");
- new (memory) A;
- PrintMemory(memory, "after placement new");
- reinterpret_cast<A *>(memory)->~A();
- system("pause");
- return 0;
- };
复制代码 帮忙看下红色(39行new (memory) A;)这句该如何理解啊?原文http://www.cplusplus.com/articles/iy6AC542/ |
|