Keep these four sublanguages in mind, and don’t be surprised when you encounter situations where effective programming requires that you change strategy when you switch from one sublanguage to another. For example, pass-by-value is generally more efficient than pass-by-reference for built-in (i.e., C-like) types, but when you move from the C part of C++(www.cppentry.com) to Object-Oriented C++(www.cppentry.com), the existence of userdefined constructors and destructors means that pass-by-referenceto- const is usually better. This is especially the case when working in Template C++(www.cppentry.com), because there, you don’t even know the type of object you’re dealing with. When you cross into the STL, however, you know that iterators and function objects are modeled on pointers in C, so for iterators and function objects in the STL, the old C pass-by-value rule applies again. (For all the details on choosing among parameter-passing options, see Item 20.)
C++(www.cppentry.com), then, isn’t a unified language with a single set of rules; it’s a federation of four sublanguages, each with its own conventions. Keep these sublanguages in mind, and you’ll find that C++(www.cppentry.com) is a lot easier to understand.
Things to Remember
Rules for effective C++(www.cppentry.com) programming vary, depending on the part of C++(www.cppentry.com) you are using.
定义你想怎么使用C++(www.cppentry.com)非常重要,这决定了你的项目是否能够一直做下去直到发布。就算只有你一个人做项目,你也会使用别人的代码(至少是标准库),或提供扩展接口供别人编写扩展。这都会和并非出自你手的代码打交道。即使所有的一切都是由你一个人掌握,你也不可能随心所欲地使用那些C++(www.cppentry.com)中看起来最酷的特性,因为你总会发现C++(www.cppentry.com)中还有更有趣的东西可供挖掘。这种想法很危险,因为如此一来项目会逐渐偏离原始的目标,编写C++(www.cppentry.com)代码只是为了用C++(www.cppentry.com)编写,而非为了解决问题。