When you specify the return type of a function, you usually specify an addressing mode only if the function returns a pointer. You don't specify an addressing mode if the function returns a built-in type. For example,
char __far *func1(); // Return a far pointer to a char
__far char func2(); // Error: meaningless
In the declaration of func1(), the __far keyword modifies the pointer being returned. In the declaration of func2(), the __far keyword modifies the character being returned, which is illegal.
However, if a function returns an object, you can specify an addressing mode. With C++, you can invoke member functions for the temporary object returned by a function, even if you don't assign the object to another variable. For example,
RecArray makeArray( FILE *handle ); // Function returning
// an object
main()
{
makeArray( currFile ).printNames(); // printNames() invoked
// for temporary object
}
The RecArray object returned by makeArray() is not assigned to another object, and thus cannot be referenced after that line of code. It is used only to call the printNames() member functions.
You might need to specify the addressing mode of the object returned, if the member function you call accepts only one addressing mode. Consider the following declaration:
class __far RecArray
{
public:
RecArray();
void printNames();
void printAll() __near;
~RecArray();
private:
// ...
};
The member function printNames() can be called for far RecArray objects and for near RecArray objects through type conversion. However, the member function printAll() can be called only for near RecArray objects. Given the previous declaration of makeArray(), the statement
makeArray( currFile ).printAll();
is an error, because the compiler creates a far temporary RecArray object, and its address cannot be converted to the near address that print_all() expects.
To specify the addressing mode of a function's return type, place the class, struct, or union keyword and the addressing mode keyword (__near, __far, or __huge) before the return type, as follows:
class __near RecArray makeArray( FILE *handle );
This specifies that makeArray() returns a near RecArray object. This declaration lets you call printAll() for the return object. Note that this syntax can be used only for functions that return an instance of a user-defined type, not for functions that return built-in types.