對象的創(chuàng)建和存在時間(計算機專業(yè)外文翻譯).doc
對象的創(chuàng)建和存在時間(計算機專業(yè)外文翻譯),對象的創(chuàng)建和存在時間object landscapes and lifetimes對象的創(chuàng)建和存在時間從技術角度說,oop(面向?qū)ο蟪绦蛟O計)只是涉及抽象的數(shù)據(jù)類型、繼承以及多形性,但另一些問題也可能顯得非常重要。本節(jié)將就這些問題進行探討。最重要的問題之一是對象的創(chuàng)建及破壞方式。對象需要的數(shù)據(jù)位于哪兒,如何控制對象的“...
內(nèi)容介紹
此文檔由會員 lzymzgq 發(fā)布對象的創(chuàng)建和存在時間
Object landscapes and lifetimes
對象的創(chuàng)建和存在時間
從技術角度說,OOP(面向?qū)ο蟪绦蛟O計)只是涉及抽象的數(shù)據(jù)類型、繼承以及多形性,但另一些問題也可能顯得非常重要。本節(jié)將就這些問題進行探討。
最重要的問題之一是對象的創(chuàng)建及破壞方式。對象需要的數(shù)據(jù)位于哪兒,如何控制對象的“存在時間”呢?針對這個問題,解決的方案是各異其趣的。C++認為程序的執(zhí)行效率是最重要的一個問題,所以它允許程序員作出選擇。為獲得最快的運行速度,存儲以及存在時間可在編寫程序時決定,只需將對象放置在堆棧(有時也叫作自動或定域變量)或者靜態(tài)存儲區(qū)域即可。這樣便為存儲空間的分配和釋放提供了一個優(yōu)先級。某些情況下,這種優(yōu)先級的控制是非常有價值的。然而,我們同時也犧牲了靈活性,因為在編寫程序時,必須知道對象的準確的數(shù)量、存在時間、以及類型。如果要解決的是一個較常規(guī)的問題,如計算機輔助設計、倉儲管理或者空中交通控制,這一方法就顯得太局限了。
Object landscapes and lifetimes
Technically, OOP is just about abstract data typing, inheritance, and polymorphism, but other issues can be at least as important. The remainder of this section will cover these issues.
One of the most important factors is the way objects are created and destroyed. Where is the data for an object and how is the lifetime of the object controlled? There are different philosophies at work here. C++ takes the approach that control of efficiency is the most important issue, so it gives the programmer a choice. For maximum run-time speed, the storage and lifetime can be determined while the program is being written, by placing the objects on the stack (these are sometimes called automatic or scoped variables) or in the static storage area. This places a priority on the speed of storage allocation and release, and control of these can be very valuable in some situations. However, you sacrifice flexibility because you must know the exact quantity, lifetime, and type of objects while you're writing the program. If you are trying to solve a more general problem such as computer-aided design, warehouse management, or air-traffic control, this is too restrictive.