placementnew.cpp

来自「Think in C++ 2nd」· C++ 代码 · 共 29 行

CPP
29
字号
//: C13:PlacementNew.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// Placement with operator new

#include <cstddef> // Size_t

#include <iostream>

using namespace std;



class X {

  int i;

public:

  X(int ii = 0) : i(ii) {}

  ~X() {

    cout << "X::~X()" << endl;

  }

  void* operator new(size_t, void* loc) {

    return loc;

  }

};



int main() {

  int l[10];

  X* xp = new(l) X(47); // X at location l

  xp->X::~X(); // Explicit destructor call

  // ONLY use with placement!

} ///:~

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?