正在加载图片...
1110 EMULATING OBJECT TECHNOLOGY IN NON-O-O ENVIRONMENTS $34.4 put A C object with C remove remove function empty references item count C item (... C empty (... CLASS INSTANCE representation For example,a C structure type REAL STACK may be declared by the type definition ty pedef struct /Exported features * void (*remove)(), void (*put)() float (*item)(), BOOL (*empty)(). /Secret features (implementation)*/ int count; float representation [MAXSIZE]; REAL STACK: The braces delimit the components of the structure type;foat introduces real numbers;procedures are declared as functions with a void result type;comments are delimited by /and */The other asterisks serve to de-reference pointers;the idea in the practice of C programming is that you add enough of them until things seem to work, and if not you can always try a or two.If this still does not succeed,you will usually find someone who knows what to do. Here the last two components are an integer and an array;the others are references to functions.In the declaration as written,the comments about exported and secret features apply to the emulated class,but everything is in fact available to clients. Each instance of the type must be initialized so that the reference fields will point to appropriate functions.For example,if my stack is a variable of this type and C remove is a stack popping function,you may assign to the remove field of the my stack object a reference to this function,as follows: my_stack.remove =C remove In the class being emulated,feature remove has no argument.To enable the C remove function to access the appropriate stack object,you must declare it as1110 EMULATING OBJECT TECHNOLOGY IN NON-O-O ENVIRONMENTS §34.4 For example, a C structure type REAL_STACK may be declared by the type definition typedef struct { /∗ Exported features ∗/ void (∗remove) (); void (∗put) (); float (∗item) (); BOOL (*empty) (); /∗ Secret features (implementation) ∗/ int count; float representation [MAXSIZE]; } REAL_STACK; The braces { } delimit the components of the structure type; float introduces real numbers; procedures are declared as functions with a void result type; comments are delimited by /∗ and ∗/. The other asterisks ∗ serve to de-reference pointers; the idea in the practice of C programming is that you add enough of them until things seem to work, and if not you can always try a & or two. If this still does not succeed, you will usually find someone who knows what to do. Here the last two components are an integer and an array; the others are references to functions. In the declaration as written, the comments about exported and secret features apply to the emulated class, but everything is in fact available to clients. Each instance of the type must be initialized so that the reference fields will point to appropriate functions. For example, if my_stack is a variable of this type and C_remove is a stack popping function, you may assign to the remove field of the my_stack object a reference to this function, as follows: my_stack ● remove = C_remove In the class being emulated, feature remove has no argument. To enable the C_remove function to access the appropriate stack object, you must declare it as count C_remove (…) { …} C_put (…) { …} C_item (…) { …} C_empty (…) { …} representation put remove empty item CLASS INSTANCE A C object with function references
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有