<!--[endif]-->宁以pass-by-reference-to-const替换pass-by-value(Prefer pass-by-conference-to-const
to pass-by-value.)
pass-by-conference-to-const:
class Point
{
public:
Point( ) : xval(2), yval(2){printf("%d,%d/n", xval, yval);}
void SetXval(int i){xval = i;}
private:
int xval, yval;
};
void const_test(const Point *p)
{
p->SetXval(9);
}
Point *p = new Point();
const_test(p);
这样会编译出错:
“错误:将 ‘const Point’ 作为 ‘void Point::SetXval(int)’
的 ‘this’ 实参时丢弃了类型限定”
也就是说,“const
Point *p”,p的成员不可以发生更改。
类似,Point 的成员函数:
void SetXval(int
i)const{xval = i;}
则也会编译出错,因为在SetXval函数中,this是const的。const会修饰conference。
pass-by-value:
void const_test(Point p)
{
}
Point p;
const_test(p);
这样操作,从p到函数实参,会涉及一次copy构造函数调用。
pass-by-conference-to-const优势:
<!--[if !supportLists]-->1、
<!--[endif]-->没有任何构造函数和析构函数被调用。
<!--[if !supportLists]-->2、
<!--[endif]-->可以避免slicing(对象切割问题):当一个derived class以by-value的方式传递并被视为一个base class对象,base class的构造copy构造函数可以被调用,这样新的对象中,derived class部分会被丢弃掉,而获得一个base class对象。这样调用virtual函数是,会调用base class的,而不是预期中的derived的。
<!--[if !supportLists]-->3、
<!--[endif]-->对于内置类型对象,STL的迭代器,函数对象,pass
by-value往往比pass-by-reference-to-const
效率高些。除此之外,其他尽量以pass-by-reference-to-const代替pass-by-value。
注意:
<!--[if !supportLists]-->1、
<!--[endif]-->尽量以pass-by-reference-to-const代替pass-by-value。前者通常比较高效,并可避免切割(slicing)问题。
<!--[if !supportLists]-->2、
<!--[endif]-->以上规则并不适用内置类型对象,STL的迭代器,函数对象。对他们而言,pass-by-value往往比较适当。
分享到:
相关推荐
1、为什么要宁以pass-by-reference-to-const 替换 pass-by-value 效率方面 缺省情况下,C++以by value 方式传递对象至(或来自)函数。 除非你另外指定,否则函数参数都是以实际实参的副本为初值,而调用段所获得的...
条款20:宁以pass-by-reference-to-const替换Pass-by-value 条款21:必须返回对象时,别妄想返回其reference 条款22: 将成员变量声明为private 条款23: 宁以non-member、non-friend替换member函数 条款24:若...
条款20:宁以pass-by-reference-to-const替换pass-by-value prefer pass-by-reference-to-const to pass-by-value. 条款21:必须返回对象时,别妄想返回其reference don't try to return a reference when you must ...
因此当这四个子语言相互切换的时候,可以更多地考虑高效编程,例如pass-by-value和pass-by-reference在不同语言中效率不同 总结: C++高效编程守则视状况而变化,取决于使用哪个子语言 2. 尽量以const, enum, inline...
C++ learning ---const用法总结,将平时不同情况的const用法,进行了详细的总结。是学习C++的好帮手
1.2 条款02:尽量以const,enum,inline替换#define 这个条款或许可以改为“宁可用编译器替换预处理器”。 通常C++要求你对所使用的任何东西提供一个定义式,但如果它是个class专属常量 又是static且为整数类型...
EFFECTIVE C++ 条款03 尽量使用const 思维导图
2.const对象-NULL和nullptr-C++中创建对象数组.docx
-文件作用域(在所用函数定义之外说明的,其作用域从说明点开始,一直延伸到源文件结束)下声明的inline函数和const常量都默认为static存储类型如加上extern则为外部存储类型. 变量的存储类型: 1.auto(自动)型:存放...
2023-04-06-项目笔记-第一阶段-4.3.1 变量的使用 4.4 变量的作用域与生命周期 4.4.1 局部变量的作用域 4.4.2 全局变量的作用域 4.4.3 局部变量的生命周期 4.4.4 全局变量的生命周期 4.5.1 字面常量 4.5.2 const 修饰...
安装$ npm install --save pass-fail-messages用法const passFailMessages = require ( 'pass-fail-messages' ) ;passFailMessages . passRandom ( ) ;//=> 'The force is strong with this one.'原料药。经过类型:...
Other C++ Features Reference Arguments Function Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and ...
const的用法
UNICODE下宽字符的CString转换为const char 和char到WCHAR的相互转换
vue3-count-to基于 vue-count-to 升级改造预览目的由于原有作者好像没打算升级版本,故而自己升级维护一下。兼容当前版本同时兼容 vue2、vue3,亦能在ts下使用运行环境已经测试的运行环境,vite建议使用2.0+,...
c++ 的array源码分析和reverse-iterator和-Array-const-iterator类
Decrements the reference counter to an object. When the reference counter reaches 0, the object is released. Parameters: In: inRef - The reference of the object item. Out: None Returns: ...
安装npm install key-value-replace用法const replace = require ( 'key-value-replace' )replace ( 'hello {{ name }}' , { name : 'world' } ) // hello worldreplace ( '{{ greeting }} {{ name }}' , { greeting...
// error: nonconst reference to a const object 非const引用是指向非const类型变量的引用。 const引用可以初始化为不同类型的对象或者右值(如字面值常量),但非const引用不可以。 代码如下: //
用法import { toPng , toJpeg , toBlob } from "dom-to-image-retina"const dataUrl = await toPng ( el , options )const dataUrl = await toJpeg ( el , options )const blob = await toBlob ( el , options )执照...