构造与析构课上栗子1
程序员文章站
2024-01-29 19:36:34
...
#include <iostream>
using namespace std;
class Point{
public :
Point() :x(0), y(0) { cout << "default constructor" <<x<< endl; };
Point(int x, int y) :x(x), y(y) { cout << "myself constructor" <<x<< endl; };
~Point() { cout << "~Point" << x<<endl; };
private:
int x, y;
};
Point p;
Point *m() {
Point p1(1, 2), p2(2, 3);
static Point p3(3,4);
Point *p4 = new Point(4, 5);
Point *p5 = new Point(5, 6);
delete p4;
return p5;
}
int main()
{
Point *ptr = m();
m();
delete ptr;
return 0;
}
default constructor0
myself constructor1
myself constructor2
myself constructor3
myself constructor4
myself constructor5
~Point4
~Point2
~Point1
myself constructor1
myself constructor2
myself constructor4
myself constructor5
~Point4
~Point2
~Point1
~Point5
~Point3
~Point0
#include <iostream>
using namespace std;
class Point{
public :
Point() :x(0), y(0) { cout << "default constructor" <<x<< endl; };
Point(int x, int y) :x(x), y(y) { cout << "myself constructor" <<x<< endl; };
~Point() { cout << "~Point" << x<<endl; };
private:
int x, y;
};
Point p;
Point *m() {
Point p1(1, 2), p2(2, 3);
static Point p3(3,4);
Point *p4 = new Point(4, 5);
Point *p5 = new Point(5, 6);
delete p4;
return p5;
}
int main()
{
Point *ptr = m();
cout << "create ptr"<< endl;
cout << "m() start" << endl;
m();
cout << "m() end " << endl;
delete ptr;
cout << "delete ptr" << endl;
return 0;
}
default constructor0
myself constructor1
myself constructor2
myself constructor3
myself constructor4
myself constructor5
~Point4
~Point2
~Point1
create ptr
m start
myself constructor1
myself constructor2
myself constructor4
myself constructor5
~Point4
~Point2
~Point1
m() end
~Point5
delete ptr
~Point3
~Point0
上一篇: html、css、响应式
下一篇: laravel框架设计excel表格式