#include <iostream>
using namespace std;
class Time
{
public:
int h;
int m;
int s;
Time( int h = 0, int m = 0, int s = 0 )
{
operator()(h,m,s);
}
//小括号重载 版本0 注意和下面用户自定义转换的区别
int operator()()
{
return h*3600 + m*60 + s;
}
//用户自定义转换
operator int()
{
return h*3600 + m*60 + s;
}
//小括号重载 版本1
void operator()(int h)
{
operator()(h,0,0);
}
//小括号重载 版本2
void operator()(int h, int m)
{
operator()(h,m,0);
}
//小括号重载 版本3
void operator()(int h, int m, int s)
{
this->h = h;
this->m = m;
this->s = s;
}
friend ostream & operator << ( ostream & os, const Time & t )
{
os << t.h << "::";
if ( t.m < 10 )
{
os << '0';
}
os << t.m << "::";
if ( t.s < 10 )
{
os << '0';
}
os << t.s;
return os;
}
};
int main()
{
/*
** 注意:t(1),t(1,1),t(1,1,1)的用法
** 像不像“函数调用”
** 这样的用法称之为仿函数
** 仿函数在STL里用得特别多
*/
Time t;
cout << t << endl;
t(1); //小括号重载 版本1
cout << t << endl;
t(1,1); //小括号重载 版本2
cout << t << endl;
t(1,1,1); //小括号重载 版本3
cout << t << endl;
cout << t() << endl; //小括号重载 版本0
cout << int(t) << endl; //用户自定义转换
return 0;
}