| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 470 人关注过本帖
标题:赋值问题
只看楼主 加入收藏
parksun
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-11-14
收藏
 问题点数:0 回复次数:4 
赋值问题

#include<iostream>
using namespace std;
class Time{
public:
Time();
Time(int hour,int minute,int second);
Time(const Time& t);
~Time();
void showTime();
private:
int hrs,mins,secs;
};
Time::Time()
{ hrs=0;mins=0;secs= 0; }
Time::Time(int hour,int minute,int second){
hrs=hour;
mins=minute;
secs=second;
}
Time::Time(const Time& t){
Time s(t);
hrs=s.hrs;
mins=s.mins;
secs=s.secs;
}
Time::~Time()
{cout<<"Destruction is called"<<endl;}
void Time::showTime()
{cout<<hrs<<":"<<mins<<":"<<secs<<endl;}
int main(){
cout<<"<<<Object Tnitiallizations uing the\''copy constructor\">>>"<<endl;
Time aTime(10,20,30);
cout<<"the Time stored in aTime is";
aTime.showTime();
Time bTime;
bTime=aTime;
cout<<"the Time stored in bTime is";
bTime.showTime();
Time cTime;
cTime=bTime;
cout<<"the Time stored in cTime is";
cTime.showTime();
cout<<"End of my act!"<<endl;
return 0;
}
把它Time bTime;bTime=aTime;Time cTime;cTime=bTime;变成Time bTime(aTime);Time cTime(bTime)或者Time bTime=aTime;Time cTime=bTime为什么运行得不到想要结果了那?

搜索更多相关主题的帖子: 赋值 
2007-11-17 14:22
neverDie
Rank: 1
等 级:新手上路
威 望:1
帖 子:123
专家分:0
注 册:2007-5-5
收藏
得分:0 
Time::Time(const Time& t){
Time s(t);
hrs=s.hrs;
mins=s.mins;
secs=s.secs;
}
你的copy constructor真奇怪

2007-11-17 14:33
parksun
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-11-14
收藏
得分:0 
回复:(neverDie)Time::Time(const Time& t){ ...
Time::Time(const Time& t){
Time s(t);
hrs=s.hrs;
mins=s.mins;
secs=s.secs;
}
我的意思是把s=t;不知道会不会以s(t)去构造s......
2007-11-17 14:57
duccdd
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-25
收藏
得分:0 
Time::Time(const Time& t){
Time s(t);
hrs=s.hrs;
mins=s.mins;
secs=s.secs;
}
递归调用构造函数,没见过,这样可以吗?
2007-11-17 22:20
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

接楼上
不可以。
Time s(t);多余

更严格的说这里的复制构造函数多余。


Fight  to win  or  die...
2007-11-17 23:07
快速回复:赋值问题
数据加载中...
 
   



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

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