Effective c++ 3/e item 15 疑問解惑

来源:互联网 发布:河南省进出口数据 编辑:程序博客网 时间:2024/06/05 23:34

轉載自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 firstoperator 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 secondoperator(), 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();
0 0