CLASSOF Statement
Statement: Declares one or more polymorphic objects with the same declared type and type parameters of another data object.
CLASSOF(data-obj( [[, attr-list] ::] v-list
data-obj |
Is a data object whose type and type parameters have been previously declared or established by implicit typing rules. If it has the OPTIONAL attribute, it cannot have a deferred or an assumed type parameter. It cannot have an intrinsic type as its declared type, or be of assumed type. It need not itself be polymorphic. |
attr-list |
Is a comma separated list of one or more attributes att listed on the type declarations page. |
v-list |
Is a comma separated list of one or more variables v as described on the type declarations page. Each v must be a dummy argument or have the POINTER or ALLOCATABLE attribute. |
A CLASSOF statement may appear only in a type declaration statement or a component definition statement.
Each data object v is polymorphic with the same declared type and type parameters as data-obj, except if a type parameter of data-obj is deferred, the corresponding type parameter of v is deferred. If data-obj is declared as CLASS(*), the CLASSOF type declaration is the same as a CLASS(*) type declaration.
Example
A generic macro to swap two polymorphic objects could be defined using fpp as
#define POLY_SWAP (arg1, arg2) \
ASSOCIATE (a1 => arg1, a2 +> arg2);\
BLOCK; \
CLASSOF(a1),ALLOCATABLE :: tmp; \
tmp = a1; \
a1 = a2; \
a2 = tmp; \
END BLOCK; \
END ASSOCIATE