关于析构函数,,delete。

fukang258369 2011-04-02 10:04:46
刚学了析构函数,,写这程序的目的是为了体验一下它的作用。。
class A
{
private:
int *p;
int a,b,c;
public:
A(int,int,int);
~A();
};
A::A(int d,int e,int f)
{
a=d;
p=new int;//--------------------------------------------
b=e;
c=f;
cout<<"call construtor!"<<endl;
}
A::~A()
{
cout<<"call destructor!"<<endl;//运行时就是不显示这一行,,为什么单步调试时候就显示呢。。
delete p;//----------------------------------------------
}

int main()
{
A a(1,2,3);
return 0;
}

可能还是对构析函数没有什么了解。。
...全文
175 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
sfido 2011-04-03
  • 打赏
  • 举报
回复
用cmd 命令行显示
xyq129052 2011-04-03
  • 打赏
  • 举报
回复
怎么能不显示呢?我复制你的代码的结果是可以显示call construtor
call destructor!
fukang258369 2011-04-03
  • 打赏
  • 举报
回复
额,,我明白了。。
zzxlsrl 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 pengzhixi 的回复:]
引用 9 楼 fukang258369 的回复:
都不相信我,,真加了。。还是不行。。

int main()
{
{
A a(1,2,3);
}
system("pause");
return 0;
}
好吧这样加个括号吧。反正那个方法你不清楚
[/Quote]
+++
fukang258369 2011-04-02
  • 打赏
  • 举报
回复
应该是编译器的原因。。
fukang258369 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 pengzhixi 的回复:]
引用 9 楼 fukang258369 的回复:
都不相信我,,真加了。。还是不行。。

int main()
{
{
A a(1,2,3);
}
system("pause");
return 0;
}
好吧这样加个括号吧。反正那个方法你不清楚
[/Quote]


偶像啊,,这什么情况?
ds_guteng 2011-04-02
  • 打赏
  • 举报
回复
#include<iostream>
#include <stdlib.h>

using namespace std;


class A
{
private:
int *p;
int a,b,c;
public:
A(int,int,int);
~A();
};
A::A(int d,int e,int f)
{
a=d;
p=new int;//--------------------------------------------
b=e;
c=f;
cout<<"call construtor!"<<endl;
}
A::~A()
{
cout<<"call destructor!"<<endl;//运行时就是不显示这一行,,为什么单步调试时候就显示呢。。
delete p;//----------------------------------------------
}

int main()
{
A a(1,2,3);
return 0;

system("pause");
return 0;
}







这样就好了
……
pengzhixi 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fukang258369 的回复:]
都不相信我,,真加了。。还是不行。。
[/Quote]
int main()
{
{
A a(1,2,3);
}
system("pause");
return 0;
}
好吧这样加个括号吧。反正那个方法你不清楚
fukang258369 2011-04-02
  • 打赏
  • 举报
回复
都不相信我,,真加了。。还是不行。。
purewqk 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hnuqinhuan 的回复:]
你加一个暂停的语句就可以了
int main()
{
A a(1,2,3);
system("pause");
return 0;
}
还要加一个头文件 #include <stdlib.h>
[/Quote]
up...
fukang258369 2011-04-02
  • 打赏
  • 举报
回复
我加了,system("pause");,还是不行。。
pengzhixi 2011-04-02
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;


class A
{
private:
int *p;
int a,b,c;
public:
A(int,int,int);
~A();
};
A::A(int d,int e,int f)
{
a=d;
p=new int;//--------------------------------------------
b=e;
c=f;
cout<<"call construtor!"<<endl;
}
A::~A()
{
cout<<"call destructor!"<<endl;//运行时就是不显示这一行,,为什么单步调试时候就显示呢。。
delete p;//----------------------------------------------
}

int main()
{
A a(1,2,3);
return 0;

system("pause");
return 0;
}
pengzhixi 2011-04-02
  • 打赏
  • 举报
回复
vs2008下在return 0;那句加个断点就可以了
pengzhixi 2011-04-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fukang258369 的回复:]
不是,,我rerun 0;前边有句,_getch();
[/Quote]
额 你这个肯定不行的。
fukang258369 2011-04-02
  • 打赏
  • 举报
回复
不是,,我rerun 0;前边有句,_getch();
無_1024 2011-04-02
  • 打赏
  • 举报
回复
你加一个暂停的语句就可以了
int main()
{
A a(1,2,3);
system("pause");
return 0;
}
还要加一个头文件 #include <stdlib.h>

pengzhixi 2011-04-02
  • 打赏
  • 举报
回复
额 是因为太快了,你没看到那句输出

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧