IT评测·应用市场-qidao123.com技术社区

标题: 现代 C++ |C++ 基本概况 |Microsoft C/C++ 文档 学习条记 [打印本页]

作者: 圆咕噜咕噜    时间: 2024-10-30 04:36
标题: 现代 C++ |C++ 基本概况 |Microsoft C/C++ 文档 学习条记
资源和智能指针


  1. #include <memory>
  2. class widget
  3. {
  4. private:
  5.     std::unique_ptr<int[]> data;
  6. public:
  7.     widget(const int size) { data = std::make_unique<int[]>(size); }
  8.     void do_something() {}
  9. };
  10. void functionUsingWidget() {
  11.     widget w(1000000);  // lifetime automatically tied to enclosing scope
  12.                         // constructs w, including the w.data gadget member
  13.     // ...
  14.     w.do_something();
  15.     // ...
  16. } // automatic destruction and deallocation for w and w.data
复制代码
请尽可能地使用智能指针管理堆内存。 如果必须显式使用 new 和 delete 运算符,请遵循 RAII 原则。 
(初学C++,许多知识还不懂,我会专心学习并实践,将所有的学习效果分享到此平台。感谢支持。)


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4