VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 595|回复: 2

这道析构函数调用次数的题究竟哪里出了问题?

[复制链接]
29_avatar_middle
最佳答案
0 
在线会员 发表于 2020-4-21 15:49:43 | 显示全部楼层 |阅读模式
  1. #include  <iostream>
  2.    
  3. using  namespace  std;
  4. class  Student
  5. {
  6. private:
  7.         int  id;
  8.    
  9. public:
  10.         Student(const  int  a)  {   
  11.                 id  =  a;   
  12.                 cout<<"Constructor"<<endl;
  13.         }
  14.                                                                                                                                  
  15.         Student(const  Student  &  stu)  {
  16.                 id  =  stu.id;   
  17.                 cout<<"Copy  Function"<<endl;
  18.         }   

  19.         ~Student()  {   
  20.                 cout<<"Destructor"<<endl;   
  21.         }   
  22.    
  23.         void  print(Student  stu){                                                                     
  24.                 cout<<stu.id<<endl;
  25.         }
  26.         void  print(){
  27.                 cout<<id<<endl;
  28.         }
  29.         friend  void  print1(Student  stu);   
  30. };

  31. void  print1(Student  stu){     
  32.         cout<<stu.id<<endl;
  33. }
复制代码

题目要求实现类似于下面的输出格式:
输入:
a b
输出:
Constructor
stu1
a
Copy Function
a
Destructor
stu2
Constructor
b
Destructor
Copy Function
stu3
a
Copy Function
a
Destructor
Destructor
Destructor
为什么我在编写主函数的时候无法实现这样的格式呢?要求补全主函数,利用类中的函数,实现以下样例的输出,其中只有stu1,stu2,stu3使用cout输出。




上一篇:一道静态联编和动态联编的题
下一篇:C++菜鸟
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-21 17:00:21 | 显示全部楼层
俩个号码???另外一个帖子也问了
  1. int main()
  2. {
  3.         Student stu1(0);                        //Constructor
  4.         cout << "stu1" << endl;                //stu1
  5.         stu1.print();                                //0
  6.         stu1.print(stu1);                        //Copy Function                        0                        Destructor

  7.         cout << "stu2" << endl;                                //stu2
  8.         Student *stu2 = new Student(1);                //Constructor
  9.         stu2->print();                                                //1
  10.         delete stu2;                                                //Destructor


  11.         Student stu3(stu1);                                        //Copy Function
  12.         cout << "stu3" << endl;                                //stu3
  13.         stu3.print();                                                //0
  14.         stu3.print(stu3);                                        //Copy Function                        0                        Destructor

  15.         return 0;                                                //运行到此处再步进调试,则可以看到最后俩个Destructor。依次析构stu3和stu1。此处析构顺序牵扯到栈,自行查阅知识点
  16. }
复制代码
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-21 17:03:04 | 显示全部楼层
JLDawson 发表于 2020-4-21 17:00
俩个号码???另外一个帖子也问了

补充:楼主要求有输入,我写的这段代码没有输入。楼主可参考实现
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-9-29 15:20

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表