| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1640 人关注过本帖
标题:类的对象作为函数参数而出现的问题
只看楼主 加入收藏
幽园香客
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:231
专家分:142
注 册:2006-2-27
结帖率:50%
收藏
 问题点数:0 回复次数:8 
类的对象作为函数参数而出现的问题
将类的对象作为函数参数,测试时出现了奇怪的结果。
#include "iostream.h"
class test
{
public:
test(){cout<<"test construction"<<endl;}
~test(){cout<<"test destruction"<<endl;}
}
class A
{
public:
A(test s){cout<<"A construction"<<endl;}
~A(){cout<<"A destruction"<<endl;}
}
void main()
{
test a;
A(a);
return 0;
}
结果: test construction
A construction
test destruction //注意此行,多调用一次test析构函数
A destruction
test destrcution
这是怎么回事?难道析构函数和构造函数还不是配对出现的?哪位兄弟知道,谢谢了
搜索更多相关主题的帖子: 函数 参数 对象 
2007-03-24 22:06
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
以下是引用幽园香客在2007-3-24 22:06:24的发言:
将类的对象作为函数参数,测试时出现了奇怪的结果。
#include "iostream.h"
class test
{
public:
test(){cout<<"test construction"<<endl;}
~test(){cout<<"test destruction"<<endl;}
}
class A
{
public:
A(test s){cout<<"A construction"<<endl;} 是这个临时变量的析构函数以后要注意这个问题
// A(test& s){cout<<"A construction"<<endl;} 这样就是你要的
~A(){cout<<"A destruction"<<endl;}
}
void main()
{
test a;
A(a);
return 0;
}
结果: test construction
A construction
test destruction //注意此行,多调用一次test析构函数
A destruction
test destrcution
这是怎么回事?难道析构函数和构造函数还不是配对出现的?哪位兄弟知道,谢谢了


嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2007-03-24 22:20
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
嗯,试了一下确实有这个问题,查查C++的规范巴,才疏学浅没能解决,抱歉

http://kongfuziandlife. http://codeanddesign.
2007-03-24 22:26
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
以下是引用zinking在2007-3-24 22:26:26的发言:
嗯,试了一下确实有这个问题,查查C++的规范巴,才疏学浅没能解决,抱歉

。。。
是我么
我没有编译器
(C++不会忘这么多吧)


嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2007-03-24 22:34
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 

#include "iostream.h"
class test
{
public:
test(){cout<<"test construction"<<endl;}
test(test& t){cout << "test copy construction"<<endl;}
~test(){cout<<"test destruction"<<endl;}
};
class A
{
public:
A(test s){cout<<"A construction"<<endl;}
~A(){cout<<"A destruction"<<endl;}
};

int main()
{
test a;
A *b=new A(a);

delete b;

return 0;
}
你试一下就明白了


http://kongfuziandlife. http://codeanddesign.
2007-03-24 22:45
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
仔细想了一下之后弄明白了

http://kongfuziandlife. http://codeanddesign.
2007-03-24 22:46
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
test construction
test copy construction
A construction
test destruction
A destruction
test destruction

是这个吧

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2007-03-24 22:53
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-03-24 23:08
幽园香客
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:231
专家分:142
注 册:2006-2-27
收藏
得分:0 
谢谢楼上的几位版主!呵呵,昨天晚上可能是糊涂了,竟然忘了考虑默认拷贝构造函数了。

[此贴子已经被作者于2007-3-25 10:29:43编辑过]



做个有用的人!
2007-03-25 10:28
快速回复:类的对象作为函数参数而出现的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012401 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved