C++ 动态创建对象数组
发布日期:2021-05-10 03:22:09 浏览次数:56 分类:精选文章

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

���C++������new���������������������������������������������������������������������������������������������������new���������������������������������������

new ��������� [������������];

���������

int* arr = new int[10];

������������������������������������������������������[]���������������������������[]������������������������������������������������[]���������new ������������������������������������������������������������������������������������������������������������

int* arr = new int;

������������������new ��������� [������������]���������������������������������������������������������������������������������������������������������������������������������new ��������� [������������]()���������������������������������������������()������������������������������������������������new ��������� [������������]���������������������

���������������������������������������������������������������������������������������������������������delete[]���������������������������������������������

delete[] arr;

���������������delete���������������������������[]���������������������������[]������������������������������������������

delete arr;  // ������������������������������
delete[] arr; // ���������������������������

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

#include 
using namespace std;
class Point {
private:
int x, y;
public:
Point() : x(0), y(0) {
cout << "������������������������." << endl;
}
Point(int x, int y) : x(x), y(y) {
cout << "������������������������." << endl;
}
~Point() {
cout << "������������������." << endl;
}
int getX() const {
return x;
}
int getY() const {
return y;
}
void move(int x, int y) {
this->x = x;
this->y = y;
}
};
int main() {
Point* point = new Point[2];
// ������������������
// ���������������������Point* point = new Point[2]()
point[0].move(25, 15); // ���������������������������������������
point[1].move(35, 45); // ���������������������������������������
delete[] point; // ������������������������
return 0;
}

���������new������������������������������������������������

  • ������������������������������[]������������������������������������
  • ���������������delete[]������������������
  • ���������������������������()������������������������������������������������������������������
  • ������������������������������������������������������������������������
  • ���������������������������������������new���delete������������������������������������������������������������������������unique_ptr���������������������������������������

    上一篇:C++ 用vector创建数组对象
    下一篇:C++ 平面点类

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月08日 21时10分47秒