f78
In GNU C++, you can use the keyword, signature, to define a completely abstract class interface as a datatype. You can connect this abstraction with actual classes using signature pointers. If you want to use signatures, run the GNU compiler with the command-line option, -fhandle-signatures. (With this option, the compiler reserves a second keyword, sigof, as well, for a future extension.)
Roughly, signatures are type abstractions or interfaces of classes. Some other languages have similar facilities. C++ signatures are related to ML’s signatures, Haskell’s type classes, definition modules in Modula-2, interface modules in Modula-3, abstract types in Emerald, type modules in Trellis/Owl, categories in Scratchpad II, and types in POOL-I. For a more detailed discussion of signatures, see Signatures: A Language Extension for Improving Type Abstraction and Subtype Polymorphism in C++ by Gerald Baumgartner and Vincent F. Russo (Tech report CSD–TR–95–051, Dept. of Computer Sciences, Purdue University, August 1995, a slightly improved version appeared in Software—Practice & Experience, 25(8), pp. 863–889, August 1995). You can get the tech report by anonymous FTP from ftp.cs.purdue.edu in ‘pub/gb/Signature-design.ps.gz’.
Syntactically, a signature declaration is a collection of member function declarations and nested type declarations. For example, the following signature declaration defines a new abstract type ‘S’ with member functions, ‘int foo()’ and ‘int bar(int)’.
signature S { int foo (); int bar (int); };Since signature types do not include implementation definitions, you cannot write an instance of a signature directly. Instead, you can define a pointer to any class that contains the required interfaces as a signature pointer. Such a class implements the signature type.
To use a class as an implementation
of S For example, suppose that
‘C’
is a class that meets the requirements of signature ‘S’
(C
conforms to S).
Then the following statement defines a signature pointer ‘p’
and initializes it to point to an object of type ‘C’.
Abstract virtual classes
provide somewhat similar facilities in standard C++. There are two main
advantages to using signatures instead.
C obj;
S * p = &obj;
The member function call, int
i = p->foo ();,
executes obj.foo ().
signature T
{
int f (int);
int f0 () { return f (0); };
};