Here's the ideone code: http://ideone.com/Qp8Eqg
My question is, is it possible to force a conversion based on the lvalue alone? For example,
[Seconds] s = 2_h + 60_s;
cout <<s.getSeconds()<<endl;
Obviously, I would have to write something like 2_h.toSeconds(), but that would be too verbose and doesn't achieve the idea.
To allow this (which is more likely your question than what you wrote, correct me if I'm wrong):
Seconds s = 2_h;
the following would work: Add operator Seconds() const
to class Hours
:
class Hours {
unsigned long long _hours;
public:
Hours(unsigned long long hours) : _hours(hours) { }
operator Seconds() const;
unsigned long long getHours() const {
return this->_hours;
}
};
and define it after class Seconds
:
Hours::operator Seconds() const { return this->_hours * 3600; }