正在加载图片...
$34.4 OBJECT-ORIENTED PROGRAMMING AND C 1111 C remove(s) REAL STACK s; ..Implementation of remove operation .. } so that a client may apply remove to a stack my stack under the form my_stack.remove (my stack) More generally,a routine rout which would have n arguments in the class will yield a C function C rout with n+/arguments.An object-oriented routine call of the form x.rout (arg1,arg2,...argn) will be emulated as x.C rout (x,argl,arg2,....argn) Emulating classes Exercise E34.3.page The preceding technique will work to a certain extent.It can even be extended to 1112. emulate inheritance. But it is inapplicable to any serious development:as illustrated in the figure of the preceding page,it implies that every instance of every class physically contains references to all the routines applicable to it.The space overhead would be prohibitive, especially with inheritance. To bring this overhead down to an acceptable level,notice that the routines are the same for all instances of a class.So we may introduce for each class a run-time data structure,the class descriptor,containing references to the routines;we can implement it as a linked list or an array.The space requirements decrease dramatically:instead of one pointer per routine per object,we can use one pointer per routine per class,plus one pointer per object giving access to the class descriptor,as shown by the figure at the top of the following page. Timewise we pay the price of an indirection:as shown in the figure,you have to go through the descriptor to find the function applicable to an object.The space economy and the simplification seem well worth this penalty. There is no secret about it:the technique just sketched is what has made C useful as an implementation vehicle for object-oriented languages,starting with Objective-C and C++in the early eighties.The ability to use function pointers,combined with the idea of grouping these pointers in a class descriptor shared by an arbitrary number of instances, yields the first step towards implementing O-O techniques. This is only a first step,of course,and you must still find techniques for implementing inheritance (multiple inheritance in particular is not easy),genericity, exceptions,assertions and dynamic binding.To explain how this can be done would take another book.Let us,however,note one important property,deducible from what we have§34.4 OBJECT-ORIENTED PROGRAMMING AND C 1111 C_remove (s) REAL_STACK s; { … Implementation of remove operation … } so that a client may apply remove to a stack my_stack under the form my_stack ● remove (my_stack) More generally, a routine rout which would have n arguments in the class will yield a C function C_rout with n+1 arguments. An object-oriented routine call of the form x ● rout (arg1, arg2, …, argn) will be emulated as x ● C_rout (x, arg1, arg2, …, argn) Emulating classes The preceding technique will work to a certain extent. It can even be extended to emulate inheritance. But it is inapplicable to any serious development: as illustrated in the figure of the preceding page, it implies that every instance of every class physically contains references to all the routines applicable to it. The space overhead would be prohibitive, especially with inheritance. To bring this overhead down to an acceptable level, notice that the routines are the same for all instances of a class. So we may introduce for each class a run-time data structure, the class descriptor, containing references to the routines; we can implement it as a linked list or an array. The space requirements decrease dramatically: instead of one pointer per routine per object, we can use one pointer per routine per class, plus one pointer per object giving access to the class descriptor, as shown by the figure at the top of the following page. Timewise we pay the price of an indirection: as shown in the figure, you have to go through the descriptor to find the function applicable to an object. The space economy and the simplification seem well worth this penalty. There is no secret about it: the technique just sketched is what has made C useful as an implementation vehicle for object-oriented languages, starting with Objective-C and C++ in the early eighties. The ability to use function pointers, combined with the idea of grouping these pointers in a class descriptor shared by an arbitrary number of instances, yields the first step towards implementing O-O techniques. This is only a first step, of course, and you must still find techniques for implementing inheritance (multiple inheritance in particular is not easy), genericity, exceptions, assertions and dynamic binding. To explain how this can be done would take another book. Let us, however, note one important property, deducible from what we have Exercise E34.3, page 1112
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有