L.L.569 2021-12-28 16:45 采纳率: 100%
浏览 22
已结题

析构函数的运行原理的问题

问题遇到的现象和发生背景

析构函数析构了哪三次?不懂析构函数怎么运行的

问题相关代码,请勿粘贴截图
 #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

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 书山客 2021-12-28 16:53
    关注

    第一次是函数fun1(Point p) 运行结束时析构p
    第二次是main结束时析构b
    第三次是运行结束析构a

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月28日
  • 已采纳回答 12月28日
  • 创建了问题 12月28日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改