Professional C++读书笔记03

C++编程规范与最佳实践
本文详细介绍了C++编程中的文件注释、变量命名、类访问权限、构造函数及对象初始化等内容,提供了清晰的代码组织和设计原则,旨在帮助开发者提高代码质量和可维护性。

Chapter07

1、前缀注释:在源文件前面加上的标准注释,可以加入

         文件/类名/;最后一次修改时间;原作者;文件/类的简单描述;未完成的特性;已知的bug等信息

2、变量命名约定:常见前缀

前缀

示例

解释

用法

m

mData

成员 member

类中的数据成员

s

sLookUpTable

静态 static

静态变量或数据成员,类变量

k

kMaxLength

常量 konstant

指示一个常量值(也可全大写表示)

f

fCompleted

标志 flag

指示一个布尔值,表示一个yes no属性

n/m

nLines/mLines

数字 number

作为一个计数器的数据成员

tmp

tmpName

临时 temporary

一个临时变量

Chapter08

1、类默认访问权限:private        结构体默认访问权限:public  除此之外,二者相同

访问限定符

含义

何时使用

public

任何代码都可以调用一个publc方法,或访问一个对象的public成员

希望客户使用的方法要置为public

protected

当前类及子类的任何方法都可以调用protected方法或访问protected成员

不希望客户调用的方法要置为protected

大多数数据成员都置为protected

private

只有此类的方法才可调用private方法及访问成员,子类不可以

只有希望限制子类对方法或成员的访问时,才使用private

2、this指针:从对象的一个方法A中调用方法B,其中方法B的参数是对象指针

         如方法A          SetValue()中可调用方法B  PrintValue( ClassType *p)

         则可SetValue()

         {                 PrintValue(this)                           }

3、在栈上,只要声明对象就会调用构造函数  Student s1(10);

         在堆上,可以声明一个指针,但不必立即调用

                            Student *ps;   

                            ps=new Student(10);        //此处才调用构造函数

         不要尝试从类的一个构造函数调用另一个构造函数

4、默认构造函数:无参构造函数

         例如SpreadsheetCell ::SpreadsheetCell()

                   {        mValue=0;                 mString =””;

                   }

         在栈上使用默认构造函数时要去掉(),例如SpreadsheetCell myCell;//调用默认构造函数

                                                                                    myCell.setValue(5);                   //使用方法

         在堆上使用,必须采用加()的方法,如SpreadsheetCell *myCell=new SpreadsheetCell();

         注意:一旦自己写了构造函数(即使不是零参),编译器也不再生成默认构造函数

         ∴若构造函数只写了一个一参的,则SpreadsheetCell myCell;的形式会出错(不再生成无参)

转载于:https://www.cnblogs.com/yangtianxing/archive/2012/05/20/2510994.html

By 作者: Marc Gregoire ISBN-10 书号: 1119421306 ISBN-13 书号: 9781119421306 Edition 版本: 4 出版日期: 2018-04-17 pages 页数: 1176 内容简介: Get up to date quickly on the new changes coming with C++17 Professional C++ is the advanced manual for C++ programming. Designed to help experienced developers get more out of the latest release, this book skims over the basics and dives right in to exploiting the full capabilities of C++17. Each feature is explained by example, each including actual code snippets that you can plug into your own applications. Case studies include extensive, working code that has been tested on Windows and Linux, and the author’s expert tips, tricks, and workarounds can dramatically enhance your workflow. Even many experienced developers have never fully explored the boundaries of the language’s capabilities; this book reveals the advanced features you never knew about, and drills down to show you how to turn these features into real-world solutions. The C++17 release includes changes that impact the way you work with C++; this new fourth edition covers them all, including nested namespaces, structured bindings, string_view, template argument deduction for constructors, parallel algorithms, generalized sum algorithms, Boyer-Moore string searching, string conversion primitives, a filesystem API, clamping values, optional values, the variant type, the any type, and more. Clear explanations and professional-level depth make this book an invaluable resource for any professional needing to get up to date quickly. Maximize C++ capabilities with effective design solutions Master little-known elements and learn what to avoid Adopt new workarounds and testing/debugging best practices Utilize real-world program segments in your own applications C++ is notoriously complex, and whether you use it for gaming or business, maximizing its functionality means keeping up to date with the latest changes. Whether these changes enhance your work or make it harder depends on how wel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值