这两code 有啥不同阿?

martinuk 2009-01-16 11:44:22

//--------版本1-------------
//button 是一个class
Button* b[3];

void MainWindow::bh0(){
std::printf("button_0 pressed\n");
destoryButton(b[1]);
}
void MainWindow::destoryButton(Button* b)
{
if (b!=NULL){
delete b;
b = NULL;
}
}
//------版本2------------
//button 是一个class
Button* b[3];

void MainWindow::bh0(){
std::printf("button_0 pressed\n");
if(b[1]!=NULL){
delete b[1];
b[1] =NULL;
}
}

都可以编译成功,但运行时 版本1 错误。
...全文
152 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
martinuk 2009-01-17
  • 打赏
  • 举报
回复
much clear now...
thanks.
xiaoyisnail 2009-01-17
  • 打赏
  • 举报
回复
you can use '&' to pass argument by reference, in this case, the parameter b is an alternative name of the argument, all operations on b are actually operations on the underlying argument
xiaoyisnail 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 martinuk 的回复:]
what does this magic "&" do ?

b is a an array of pointer to Button;
b[1] is the pointer to Button;which is initialized by new Button(.....);

so doesn't "delete b[1]" delete the Button object holding by b[1]?

[/Quote]
"delete b[1]" does delete the Button object pointed by b[1]
but, in function "MainWindow::destoryButton",the parameter b is just a copy of the argument, the statement "b=NULL" doesn't change the argument
martinuk 2009-01-17
  • 打赏
  • 举报
回复
what does this magic "&" do ?

b is a an array of pointer to Button;
b[1] is the pointer to Button;which is initialized by new Button(.....);

so doesn't "delete b[1]" delete the Button object holding by b[1]?
lin_style 2009-01-17
  • 打赏
  • 举报
回复
套多?

在主函数中,先输出要delete指针的值
,然后在调用函数里再输出。。

waizqfor 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Chiyer 的回复:]
第一个版本并不会使 b[1] 为NULL
[/Quote]
up
xiaoyisnail 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 martinuk 的回复:]
引用 3 楼 Chiyer 的回复:
第一个版本并不会使 b[1] 为NULL


那怎样才能只delete array中一个element 比如说b[1], 然后将其设为NULL呢?
[/Quote]

引用传参
void MainWindow::destoryButton(Button* &b)
nineforever 2009-01-17
  • 打赏
  • 举报
回复
第一个是给函数参数b赋0,不会影响全局的b数组
第二个是给全局b数组的b[1]赋0
martinuk 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 Chiyer 的回复:]
引用 4 楼 martinuk 的回复:
引用 3 楼 Chiyer 的回复:
第一个版本并不会使 b[1] 为NULL


那怎样才能只delete array中一个element 比如说b[1], 然后将其设为NULL呢?


C/C++ codevoidMainWindow::destoryButton(Button*&b)
[/Quote]

Thanks, but Why
星羽 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 martinuk 的回复:]
引用 3 楼 Chiyer 的回复:
第一个版本并不会使 b[1] 为NULL


那怎样才能只delete array中一个element 比如说b[1], 然后将其设为NULL呢?
[/Quote]


void MainWindow::destoryButton(Button*& b)

xiaoyisnail 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Chiyer 的回复:]
第一个版本并不会使 b[1] 为NULL
[/Quote]

正确,传值传参的,忽略了
martinuk 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Chiyer 的回复:]
第一个版本并不会使 b[1] 为NULL
[/Quote]

那怎样才能只delete array中一个element 比如说b[1], 然后将其设为NULL呢?
星羽 2009-01-16
  • 打赏
  • 举报
回复
第一个版本并不会使 b[1] 为NULL
xiaoyisnail 2009-01-16
  • 打赏
  • 举报
回复
你给的代码效果上没区别
fox000002 2009-01-16
  • 打赏
  • 举报
回复
第一个 lz 居然要 delete 数组,怎能不出错

65,211

社区成员

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

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