轉載自http://stackoverflow.com/questions/4728160/thrown-off-by-functor-syntax-in-effective-c
The first
operator FontHandle() const {return f;}
The second
FontHandle operator()() const {return f;}
The first, operator FontHandle
, is a conversion operator. It allows an instance of this class type to be implicitly converted to a FontHandle
object, so you could write:
Font myFont; FontHandle handle = myFont;
More commonly, conversion operators are used to allow you to use an object of one type as if it were another type in an expression. For example,
void f(FontHandle fh); Font myFont; f(myFont); // converts myFont to a FontHandle via the conversion operator
The second, operator()
, is an overload of the function call operator. It allows an instance of your class type to be used as if it were a function taking no arguments:
Font myFont; myFont();