正在加载图片...
$34.4 OBJECT-ORIENTED PROGRAMMING AND C 1107 This style of programming can be applied successfully to emulate the encapsulation techniques of Ada or Modula-2 in contexts where you have no choice but to use Fortran. It suffers of course from stringent limitations: No intemal calls are permitted:whereas routines in an object-oriented class usually rely on each other for their implementations,an entry call issued by another entry of the same subroutine would be understood as an instance ofrecursion-anathema to Fortran,and run-time disaster in many implementations. As noted,the mechanism is strictly static,supporting only one abstract object.It may be generalized to allow for a fixed num ber ofobjects(by transforming every variable into a one-dimensional array,and adding a dimension to every array).But there is no portable support for dynamic object creation. In practice,it seems that some Fortran environments(two decades after Fortran 77 was published!)do not deal too well with multiple-entry subroutines;in particular debuggers do not always know how to keep track of multiple entries.Before applying this technique to a production development,check with the local Fortran guru to find out whether it is wise to rely on this facility in your environment. Finally,the very idea of hijacking a language mechanism for purposes other than its probable design objective raises dangers of confusion and errors. 34.4 OBJECT-ORIENTED PROGRAMMING AND C Born in a log cabinet,C quickly rose to prominence.Although most people interested in both C and object technology have focused on the O-O extensions of C discussed in the next chapter(C++,Objective-C,Java),it remains interesting to see how C itself can be made to emulate O-O concepts,if only to understand the techniques that have made C so useful as a stepping stone towards the implementation of more advanced languages. Some context C was designed at AT&T's Bell Laboratories as a portable language for writing operating systems.The first version of Unix had used assembly language,but a portable version soon appeared necessary,and C was designed around 1970 to make it possible.It was derived from ideas found in BCPL,a language of the sixties which,like C,can be mentioned in the same breath as "high-level”,“machine-oriented”and“portable”:high- level thanks to control structures comparable to those of Algol or Pascal;machine- oriented because you can manipulate data at the most elementary level,through addresses, pointers and bytes;portable because the machine-oriented concepts are so defined as to cover a wide variety of computer types. C's timing could not have been better.In the late seventies Unix became the operating system of choice for many universities,and C spread with it.Then in the eighties the microcomputer revolution burst out,and C was ready to serve as its lingua franca- more scalable than Basic,more flexible than Pascal.At the same time Unix also enjoyed some commercial success,and along with Unix still came C.In a few years,a boutique product became the dominant language in large segments of the computing industry, including much of where the action really was.§34.4 OBJECT-ORIENTED PROGRAMMING AND C 1107 This style of programming can be applied successfully to emulate the encapsulation techniques of Ada or Modula-2 in contexts where you have no choice but to use Fortran. It suffers of course from stringent limitations: • No internal calls are permitted: whereas routines in an object-oriented class usually rely on each other for their implementations, an entry call issued by another entry of the same subroutine would be understood as an instance of recursion — anathema to Fortran, and run-time disaster in many implementations. • As noted, the mechanism is strictly static, supporting only one abstract object. It may be generalized to allow for a fixed number of objects (by transforming every variable into a one-dimensional array, and adding a dimension to every array). But there is no portable support for dynamic object creation. • In practice, it seems that some Fortran environments (two decades after Fortran 77 was published!) do not deal too well with multiple-entry subroutines; in particular debuggers do not always know how to keep track of multiple entries. Before applying this technique to a production development, check with the local Fortran guru to find out whether it is wise to rely on this facility in your environment. • Finally, the very idea of hijacking a language mechanism for purposes other than its probable design objective raises dangers of confusion and errors. 34.4 OBJECT-ORIENTED PROGRAMMING AND C Born in a log cabinet, C quickly rose to prominence. Although most people interested in both C and object technology have focused on the O-O extensions of C discussed in the next chapter (C++, Objective-C, Java), it remains interesting to see how C itself can be made to emulate O-O concepts, if only to understand the techniques that have made C so useful as a stepping stone towards the implementation of more advanced languages. Some context C was designed at AT&T’s Bell Laboratories as a portable language for writing operating systems. The first version of Unix had used assembly language, but a portable version soon appeared necessary, and C was designed around 1970 to make it possible. It was derived from ideas found in BCPL, a language of the sixties which, like C, can be mentioned in the same breath as “high-level”, “machine-oriented” and “portable”: high￾level thanks to control structures comparable to those of Algol or Pascal; machine￾oriented because you can manipulate data at the most elementary level, through addresses, pointers and bytes; portable because the machine-oriented concepts are so defined as to cover a wide variety of computer types. C’s timing could not have been better. In the late seventies Unix became the operating system of choice for many universities, and C spread with it. Then in the eighties the microcomputer revolution burst out, and C was ready to serve as its lingua franca — more scalable than Basic, more flexible than Pascal. At the same time Unix also enjoyed some commercial success, and along with Unix still came C. In a few years, a boutique product became the dominant language in large segments of the computing industry, including much of where the action really was
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有