代码如下:
#include
using namespace std;
class Clock
{
public:
Clock(int newH,int newM,int newS);
Clock();
Clock(Clock &pClk);
void SetTime(int newH=0,int newM=0,int newS=0);
void ShowTime();
private:
int hour;
int min;
int sec;
};
Clock::Clock(int newH,int newM,int newS)
{
hour = newH;
min = newM;
sec = newS;
}
Clock::Clock()
{
hour = 0;
min = 0;
sec = 0;
}
Clock::Clock(Clock &pClk)
{
hour = pClk.hour;
min = pClk.min;
sec = pClk.sec;
}
void Clock::SetTime(int newH, int newM, int newS)
{
hour = newH;
min = newM;
sec = newS;
}
void Clock::ShowTime()
{
cout << "now the time is :\n" << hour << ":" << min << ":" << sec << endl;
}
Clock func()
{
Clock clk(11,30,0);
return clk;
}
int main(void)
{
Clock clk1(10,30,0);
clk1.ShowTime();
cout << "----拷贝构造函数测试----" << endl;
Clock clk2 = func();//该语句编译不通过
//Clock clk2;
//clk2 = func();
clk2.ShowTime();
return 0;
}
用Clock clk2;clk2 = func();没问题,但合并为Clock clk2 = func();就编译不通过,在这里把类Clock简单理解为某个数据类型如int怎么行不通呢?望有大神解惑!