Home Segments Index Top Previous Next

448: Mainline

One reason why C++ requires the objects in arrays to be of the same type is that such a requirement ensures that each object occupies exactly the same amount of memory as every other object. This requirement makes it easy to compile efficient object code for locating each array object, given that object's array index.

From the perspective of software engineering, many expert programmers argue that objects in any given array should all be of the same type even in the absence of efficiency considerations. Their rationale is that associating data types with arrays ensures that you can see what sort of objects are allowed in a given array without looking inside a programmer's head.