Export (0) Print
Expand All

C++ Abstract Declarators

An abstract declarator is a declarator in which the identifier is omitted. (For related information, see Type Names and Declarators) While non-abstract declarators are used in the declaration of objects or functions, stripping out the identifier leaves only the type information. Thus, abstract declarators have the effect of modifying a base type such as char to specify more complex type such as pointer to char or array of char. As such, they always are used in combination with a base type name and only used when a pointer, array or reference type is called for (including a pointer to function returning that base type). The abstract declarators corresponding to the following are discussed in this section:

An abstract declarator is a declarator that does not declare a name — the identifier is left out. For example,

char * 

specifies the type pointer to type char. The type name consists of the base type name char plus the abstract declarator *.

Similarly, the abstract declarator for a reference is the & operator, and an array type is specified using brackets. Any type may be specified by simply applying the syntax for the declarator and removing the identifier.

char &   // reference to char
char[5] // array of char

The following is a more complicated use of an abstract declarator that declares the type pointer to a function that takes two arguments, both of type char *, and returns type char *:

char * (*)( char *, char * )

In combination with the base type specifier, abstract declarators completely declare a type. The type names constructed from abstract declarators may be used anywhere a type is called for:

// Get the size of array of 10 pointers to type char.
size_t nSize = sizeof( char *[10] );
// cast fptr to pointer to function that takes two arguments, both of type
// pointer to char
 i = (char * (*)(char*, char*)) fptr; 

The typedef Specifier may be used to avoid repeating the cumbersome syntax of abstract declarators.

Community Additions

© 2014 Microsoft