使用new和不使用new实例化C ++对象之间有什么区别?
在C++中,我们可以使用或不使用new关键字实例化类对象。如果不使用new关键字,则它类似于普通对象。这将存储在堆栈部分。范围结束时将销毁该对象。但是对于想要动态分配项目空间的情况,则可以创建该类的指针,并使用new运算符进行实例化。
在C++中,新函数用于动态分配内存。
示例
#include <iostream>
using namespace std;
class Point {
int x, y, z;
public:
Point(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}
void display() {
cout << "(" << x << ", " << y << ", " << z << ")" << endl;
}
};
int main() {
Point p1(10, 15, 20);
p1.display();
Point *ptr;
ptr = new Point(50, 60, 70);
ptr->display();
}输出结果
(10, 15, 20) (50, 60, 70)
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿