This article is one of <Effective C++> reading notes.
Part 4: Designs and Declarations
Item 20: Prefer pass-by-reference-to-const to pass-by-value.
Things to remember
- Prefer pass-by-value-to-const over pass-by-value. It’s typically more efficient and it avoids the slicing problem.
- The rule doesn’t apply to build-in types and STL iterator and function object types. For them, pass by value is usually appropriate.
- 对于正常的 C++ 程序,传值的地方传 const 引用总是没错的。对象复制不仅成本太高,正确实现对象复制也不是件容易的事。如果想把对象置入一个容器,最好是使用它的引用。
Key words
Copy-On-Write (COW):
copy-on-write is the name give to the process of identifying when a task attempts to make a change to shared information, creating a separate(private) copy of that information for the task and redirecting the task to making changes to the private copy to prevent its changes from becoming visible to all other tasks.
“Slicing” is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information - some of it is “sliced” away.
Code
|
|