C++每日一练(new/delete)

来源:互联网 发布:178个经典c语言源代码 编辑:程序博客网 时间:2024/06/07 03:55

一、今日课题

new/delete

二、实战演练

用于动态分配和撤销内存的运算符

1)有何用?

当你希望在函数调用结束前销毁对象

2)怎么用?

  • 使用new表达式

这里写图片描述

  • 使用delete表达式

这里写图片描述

  • 实战演练
#include <iostream>using namespace std;class ClassA{    int x;public:    ClassA(int x) { this->x = x; }    inline int GetX() { return x; }};int main(){    ClassA  *p = new ClassA(200);    if (!p)    {        cout << "内存分配出错!" << endl;        return 1;    }    cout << "x= " << p->GetX() << endl;    delete p;    system("pause");    return 0;}

三、C++树

这里写图片描述

10/19/2016 5:08:25 PM

0 0
原创粉丝点击