Tham khảo tài liệu 'ansi/iso c++ professional programmer's handbook phần 3', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | ANSI ISO C Professional Programmer s Handbook - Chapter 3 - Operator Overloading Secondly a conversion operator takes no arguments. Conversion operators can convert their object to any given type fundamental and user-defined alike struct DateRep legacy C code char day char month short year class Date object-oriented wrapper private DateRep dr public operator DateRep const return dr automatic conversion to DateRep extern C int transmit_date DateRep C-based communication API function int main Date d .use d transmit date object as a binary stream to a remote client int ret_stat transmit_date using legacy communication API return 0 Standard Versus User-Defined Conversions The interaction of a user-defined conversion with a standard conversion can cause undesirable surprises and side effects and therefore must be used with caution. Examine the following concrete example. A non-explicit constructor that takes a single argument is also a conversion operator which casts its argument to an object of this class. When the compiler has to resolve an overloaded function call it takes into consideration such user-defined conversions in addition to the standard ones. For example class Numeric private float f public Numeric float ff f ff constructor is also a float-to-Numeric conversion operator f void f Numeric Numeric num OK calls void f Numeric . Numeric s constructor converts argument to a Numeric object Suppose you add at a later stage another overloaded version of f file D Cool Stuff old ftp 1 1 ch03 8 von 15 14 45 47 ANSI ISO C Professional Programmer s Handbook - Chapter 3 - Operator Overloading void f double Now the same function call resolves differently f now calls f double not f Numeric This is because float is promoted to double automatically in order to match an overloaded function signature. This is a standard type conversion. On the other hand the conversion of float to Numeric is a user-defined conversion. User-defined conversions