- 1、本文档共4页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
高效率(Effective)C心得
C++ effective心得
介绍一些编写C++代码时可以使用的小技巧
1.Operator overloading
?
一般的:? c c::operator=(const c){。。。。return *this;};?? --------格式? 运算符的重载?? w = x = y = z = hello;//可以返回引用,效率高?? a+b+c+d???? //必须返回类,而不是返回引用?? c c::operator=(const c ){ 。。。。。。return *this;};???Operators that can be overloaded(可以重载的操作符)+, -, *, /, %, ^, , +=,-=,*=,/=, (), [], , ==,=,-,++,--, -*, … new, delete
Operators that can not be overloaded()不可以重载的操作符., ::, ?:, .*//Notes for ++, -- operator区别:?T operator ++() //prefix ++a?T operator++(int) //postfix a++
一般情况下几乎总要遵循operator = 输入和返回的都是类对象的引用的原则.必须返回一个对象时不要试图返回一个引用,当需要在返回引用和返回对象间做决定时,你的职责是选择可以完成正确功能的那个至于怎么让这个选择所产生的代价尽可能的小,那是编译器的生产商去想的事。
2.const成员函数不被允许修改它所在对象的任何一个数据成员。mutable在处理“bitwise - constness 限制” 问题时是一个很好的方案,但它被加入到c++标准中的时间不长,所以有的编译器可能还不支持它。explicit表示必须显示的调用该函数。
3.void including more times?#ndef#define
#endif
等价于#progma once
4? .struct???????????????????????????? #pragma pack(pop, n)struct Employee{?char cName[5];?short? nAge;??????? float? dSalary;?bool? bMarried;};sizeof(Employee) depends on struct member alignment. Default is 4 bytes alignment. sizeof(Employee) = 162 bytes data wants to start from even address, 4 bytes data wants to start from address that is 4 times, 1 byte data can start from any address我们可以用以下改变默认的对齐方式。#pragma pack(pop, n)
5。enum :It can define a set of const values.
6。In fact, C++ compiler creates a global function for each member function, and add another parameter whose type is our class.
一般位类名_函数名_参数列表
7。constructor?? ※?? deconstructor
Copy Constructor??Copy constructor is a constructor that has a parameter whose type is class reference.?and the class reference is const.eg:CRectangle( const CRectangle other)eg:?? CComplex a;?? CComplex b=a; // Copy constructor is invoked?? b=a; //Assignment operator is invoked
Converting Constructor?一般定义位explicit函数,必须被显示的调用
8。const in class
? 4 cases in a class?const int member;只能在初始化列表里进行初始化?int funct( const int nFactor,…);参数位常熟,在函数种不可被修改?const
您可能关注的文档
最近下载
- 2025年内蒙古交通职业技术学院单招职业适应性考试题库带答案.docx VIP
- 2025年中国浪涌保护器市场全景评估及发展趋势研究预测报告.docx
- 《年产30万吨合成氨工艺设计.doc VIP
- 基于SpringBoot的外卖点餐系统设计与实现-毕业论文.docx VIP
- 人工智能在小学数学教学中的应用探索教学研究课题报告.docx
- 2_第9版《内科学》之类风湿关节炎课件.ppt
- 基于EPC模式的项目管理项目协调管理24课件讲解.pptx
- 2018年广东省高职院校五年一贯制单独招生考试模拟题.docx
- 2025年DeepSeek大模型及其企业应用实践报告(企业篇)-厦门大学.pptx
- 2025年国家公务员考试行测真题答案解析(判断推理) .pdf VIP
文档评论(0)