欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

构造与析构课上栗子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
相关标签: c++基础