C++ 之析构函数
发布日期:2021-05-14 16:35:23 浏览次数:20 分类:精选文章

本文共 3315 字,大约阅读时间需要 11 分钟。

C++������������������������������������������������������������������������������������������������������������������������������������������������������������������~���������������������������������������������������������������������������������������������������������

���C++���������������������������������������������������������������������������������������������������������������new������������������������������delete���auto_ptr������������������������������������������������������������������������������������������������������������������������������������������������

���������������������

  • ���������������������������������������������������������������������������������������������������������������������using new������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������Student���������destroy()������������������������������scores���������
  • class Student {
    private:
    double * scores;
    public:
    Student(int len) {
    scores = new double[len];
    }
    ~Student() {
    delete scores;
    }
    };
    1. ������������������������������������������������������������������������������������������������������������������������������������������������������������������
    2. ������������������������������

      ������������������������������������~���������������������������������������������������������������������������������������������������������������������������������������������������������������������������

      class MyClass {
      ...
      ~MyClass();
      ...
      };

      ������������������������������������������������������������������������������������������������������������������������������������������������������������������

      ������������������������������������

      ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

      ������������

      ������������������������������C++���������������������������������������������������������������������������������������������������

      #include "Student.h"
      #include
      using namespace std;
      // ������.Student���������
      Student Student(int age) {
      // ���������������
      Student stu(age);
      cout << "���������������" << endl;
      return stu;
      }
      // ������new���������������
      Student * createStudentWithHeap() {
      Student * stu(new Student("������", "������������"));
      return stu;
      }
      int main() {
      // ���������������
      Student stu1; // ���������
      cout << "������������" << endl;
      // ������������������
      Student * stuHeap = new Student("������", "������������������");
      cout << "������������" << endl;
      // ������������
      delete stuHeap;
      return 0;
      }

      ������������

    3. ���������������������������������������������������������������������������������������stack������heap������������������������������������
    4. RAII������������������������������C++���RAII���������������������������������������������������������������������������������������������������������������������������
    5. ������������������������C++���������������������������������������������������������������������������������������������������������������������

    上一篇:C++ 之使用类创建对象
    下一篇:C++之类的成员函数和构造函数

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2025年05月02日 14时26分38秒