问题遇到的现象和发生背景
析构函数析构了哪三次?不懂析构函数怎么运行的
问题相关代码,请勿粘贴截图
#include<iostream>
using namespace std;
class Point {
public:
Point(int xx = 0, int yy = 0) {x=xx; y=yy;}
Point(Point &p);
~Point() {cout << "The destructor is called " << endl;}
int getX() { return x; }
int getY() { return y; }
private:
int x, y;
};
Point::Point(Point &p) {
x = p.x; y = p.y;
}
void fun1(Point p) { cout << p.getY() << endl; }
int main() {
Point a(7, 8);
Point b(a);
cout << b.getX() << endl;
fun1(b);
return 0;
}
运行结果及报错内容
7
8
The destructor is called
The destructor is called
The destructor is called