0

How is it possible to use an object as a parameter even though I don't have a constructor for it?

#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;

}

I thought I'd need a constructor like this for T2, but it's still working. Why?

Time(Time t) { 
    //maybe do something
    return t;
}
Rudi
  • 926
  • 2
  • 19
  • 38
  • Please read up C++ class basics in regard to construction/destruction and then read this to understand the implication of using sec_to_Time in the T2 constructor's parameter list immediately: http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c – MABVT May 07 '17 at 14:07
  • 1
    It's seriously very ugly to put the access specifiers on the same line like that... – DeiDei May 07 '17 at 14:08

0 Answers0