int &function(int)
像这样的返回方式是怎样的?
'conversion' : cannot convert from 'type1' to 'type2'
The compiler cannot cast from 'type1' to 'type2'.
The C2440 errors on lines 15 and 16 of this sample code will be qualified with the Incompatible calling conventions for UDT return value
message. (A UDT is a user-defined type, such as a class, struct, or union.) These type incompatibility errors are caused when the calling convention of a UDT specified in the return type of a forward declaration conflicts with the actual calling convention of the UDT and when a function pointer is involved.
In the example, we first have forward declarations for a struct and for a function that returns the struct; the compiler assumes that the struct uses the C++ calling convention. Then we have the struct definition, which, by default, uses the C calling convention. Since the compiler does not know the calling convention of the struct until it finishes reading the entire struct, the calling convention for the struct in the return type of get_c2 is also assumed to be C++.
The struct is followed by another function declaration that returns the struct, but at this point, the compiler knows that the struct's calling convention is C++. Similarly, the function pointer, which returns the struct, is defined after the struct definition so the compiler knows that the struct uses the C++ calling convention.
The errors occur because the function pointers, which expect to return a type using the C calling convention, are assigned to functions that expect to return a type using the C++ calling convention.
To resolve C2440 because of incompatible calling conventions, declare functions that return a UDT after the UDT definition.
[此贴子已经被作者于2005-8-6 12:43:42编辑过]