Strong Static Typing

C++ Primer 4/e 在變數這裡有一個關鍵概念:『

C++ is a statically typed language, which means that types are checked at compile time. The process by which types are checked is referred to as type-checking.

In most languages, the type of an object constrains the operations that the object can perform. If the type does not support a given operation, then an object of that type cannot perform that operation.

In C++, whether an operation is legal or not is checked at compile time. When we write an expression, the compiler checks that the objects used in the expression are used in ways that are defined by the type of the objects. If not, the compiler generates an error message; an is not produced.

As our programs, and the types we use, get more complicated, we’ll see that static type checking helps find bugs in our programs earlier. A consequence of static checking is that the type of every entity used in our programs must be known to the compiler. Hence, we must define the type of a variable before we can use that variable in our programs.』

中文版的是這樣說:『C++是個靜態型別語言,意思是型別在編譯期被檢查。型別被檢查的過程稱為型別檢驗。

在大部分語言中,物件的型別限制了該物件能夠執行的操作。如果物件的型別不支援某個操作,這個物件就不能執行那項操作。

在C++中,一個操作是否合法,將在編譯期被檢查。當我們寫下一個算式,編譯器會檢查算式裡用到的物件是否按其型別所定義的方法來使用。如果不是,編譯器會產出一個錯誤訊息,不產出可執行檔。

隨著我們的程式和使用的型別愈來愈複雜,我們會看到,靜態型別檢驗有助於早期找出程式臭蟲。靜態檢驗的一個結果是,程式用到的每一個物體的型別都必須為編譯器所知。因此我們必須在程式使用某個變數之前先定義好該變數的型別。』

PHP好像沒有這樣玩,玩久了PHP再去寫C或C++就死得很慘。

Print Friendly, PDF & Email

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料