Previous: Structures with Local Pointers Up: Data Transfer Functions Next: Pitfalls
Automatic Transfer Function Generation
If there are no local pointers or arrays in a user-defined type, then
the CC++ compiler can generate the correct transfer functions
automatically. For instance, the correct transfer functions for
class Point can be generated automatically, while those for
class Vector cannot be.
This implementation of CC++ follows these rules for automatic
transfer function generation:
- All types must have data transfer functions defined.
- The compiler can generate the correct transfer functions for
structures where all data members are basic types, global pointers, or
user-defined structures. The compiler cannot generate the correct
transfer functions for types with local pointers or arrays (even
statically sized).
- The compiler will generate transfer functions for all types
that the user does not. If the type contains a local pointer or an
array, and the user has not declared the transfer functions, a
compile-time warning will be given, and the generated transfer
function will not try to pass the local pointer or the array. This is
a warning rather than an error so that users interested only in a single
address space will not have to write data transfer functions.
- The user notifies the compiler that they will specify the
transfer functions for a type by declaring them as friends of that
type. The user should make these functions friends, even if that
friendship is not required to access the private members of the type.
A link-time error will result if these functions are declared but not
defined. A link-time error will result if these functions are defined
without being declared as friends in the type declaration.
- The compiler will generate either zero or two transfer functions
for each type. The user may not define one transfer function and have
the compiler define the other.
- When compiling multiple modules into one executable, the same
type can be declared many times. If the transfer functions for that
type are defined in each of them, a link-time error will result.
Because of this, if the CC++ compiler is going to generate the
transfer functions for a type, it does so where the first non-inline,
non-constructor member function of that type is defined. If there are
no such members, then the compiler option +ee1 will force
transfer functions to be generated for all types in that compile, at
the point where the type is declared.