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 executable file 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++就死得很惨。