C++ 之 值传递和引用传递(2)

// basic is type Quote; bulk is type Bulk_quote print_total(cout, basic, 20); // calls Quote::net_price print_total(cout, bulk, 20); // calls Bulk_quote::net_price

C++ 的动态绑定机制,使得程序直到运行时,才基于引用或指针绑定的对象类型,来选择调用哪个虚函数

小结:

直接套用 <C++ Programming Language> 中的建议和 <C++ Primer> 中对动态绑定的解释

1) use pass-by-value for small objects

2) use pass-by-const-reference to pass large values that you don’t need to modify

3) dynamic binding happens when a virtual function is called through a reference (or a pointer) to a base class

参考资料:

<C++ Programming Language_4th> ch 12.2.1

<Effective C++_3rd> item 20

<C++ Primer_5th> ch 6.2,  ch 15.1

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/438183a41b5110b8bcb86e1a00765ca5.html