C ++对象作为参数,但没有构造函数

时间:2017-05-07 14:01:50

标签: c++ constructor

即使我没有构造函数,如何将对象用作参数?

#include <iostream>
#include <string>
using namespace std;

class Time 
{
private: int hh, mm, ss;

public: static Time sec_to_Time (unsigned int sec)
{
    Time U;
    U.hh = sec / 3600;
    sec -= U.hh * 3600;
    U.mm = sec / 60;
    sec -= U.mm * 60;
    U.ss = sec;
    return (U);
}

};

int main()
{
    Time T1 = Time::sec_to_Time(60); //I understand this code,
    Time T2(Time::sec_to_Time(60)); //but why does this work?

    return 0;

}

我以为我需要这样的构造函数用于T2,但它仍然有效。为什么呢?

Time(Time t) { 
    //maybe do something
    return t;
}

0 个答案:

没有答案