Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utcs!mnetor!seismo!caip!princeton!allegra!alice!bs From: bs@alice.UUCP Newsgroups: net.lang Subject: Proc types Message-ID: <5789@alice.uUCp> Date: Mon, 14-Jul-86 11:19:22 EDT Article-I.D.: alice.5789 Posted: Mon Jul 14 11:19:22 1986 Date-Received: Mon, 14-Jul-86 23:07:33 EDT Organization: Bell Labs, Murray Hill Lines: 56 > Andy "Krazy" Glew writes: > I really like pointers to functions. Don't take them away. Of course, I want > full specification of argument types, even varargs... ofcourse, try C++: int printf(char* ...); int (*printfct)(char* ...) = &printf; (*printfct)("x = %d\n", x); (*printfct)("%s = %d\n", "x", x); > However, sometimes a pointer to a whole class of functions is too loose. > It would be nice to be able to constrain it further. The inheritance mechanism of languages like C++ can be used to express a limited, but regular and quite power full form of ``restriction'' in this context: int fct(base* p) { // "fct" perform operations on a "base" // but will accept arguments of any type derived from "base" } for example: struct base { // ... f(); g(); }; struct derived : base { // "derived" inherits base's members including f() and g() // ... h(); }; derived d = ... base b = ... fct(&d); // coerce the derived* to a base* fct(&b); fct() above can use base::f() and base::g(), but not derived::h(). If base::f() and base::g() were declared "virtual" derived could provide its own implemntations of them for fct() to use. Since type checking for pointers to functions is identical to the checking of functions this feature again applies to pointers to functions (but note that virtual functions takes care of many cases where one would otherwise use pointers to functions directly): int (*p)(base*) = &fct; (*f)(&d); (*f)(&b);