`

Simple Items

 
阅读更多

Item 7 多态基类中将析构函数声明为虚拟

1> 多态基类应该声明虚析构函数。如果一个类有任何虚函数,它就应该有一个虚析构函数。

2> 纯虚析构函数也必须有函数体。否则linker报错。

● Item 10 赋值函数要返回引用

赋值函数的特点:
1> 可以无限连:x = y = z = 15;
2> 满足右结合律:x = (y = (z = 15));

基于上面的特点,赋值操作必须返回*this。
+=、-=、*=等操作也算在内。

● Item 16 使用匹配的new和delete

std::string *stringArray = new std::string[100];
delete stringArray;

上面使用delete回收对象数组的资源,属于未定义行为。反之亦然。

需对typedef的数组注意:



应在实际使用中避免此类写法。需要动态数组的时候,使用STL里的string或vector。

Item 21 必须返回对象时别返回引用

1> 绝不要返回一个局部栈对象的指针或引用

2> 绝不要返回一个被分配的堆对象的引用。原因是caller在使用该引用时,可能会因为无法保存引用而导致资源泄漏。

3> 如果存在需要一个以上这样的对象的可能性时,绝不要返回一个局部 static 对象的指针或引用。原因:多个地方使用同一个该static对象的引用,会引起理解混乱。caller们以为多次调用是多个对象,想不到竟然其实是一个对象。

● Item 22 将数据成员声明为private

理由:
1> 通过函数实现对成员的访问控制
2> 封装后,可以在实现上有自由度,来适应客户的实际需要
3> 只允许你的成员函数访问它们,你就有权力在以后修改它们。一旦声明为public或protected,就变成客户的东西了,你不能改

● Item 24 需要对所有参数实施类型转换的话,使用非成员函数

Item 28 避免返回对象内部构件的句柄

所谓句柄,就是引用,指针,或STL中的迭代器。返回对象内部的句柄有可能会产生空悬句柄。比如下面返回的Point引用。

● Item 32 公有继承是is-a的关系
● Item 38 通过“合成”来实现附属关系、附属功能

分享到:
评论

相关推荐

    Unity各种日用生活物品模型Simple Items – Cartoon Assets1.04

    Unity各种日用生活物品模型Simple Items – Cartoon Assets1.04 仅供学习,请勿商用。

    QuickBooks 2019 All-in-One For Dummies

    No stone is left unturned, giving you everything you need to turn what used to be harrowing tasks into simple items you can check off on your to-do list. Helps you use QuickBooks to ease accounting ...

    Lucene+Nutch搜索源码

    Lucene+nuctch一书的全部源码 测试源码 和几个简单的项目 (Lucene+ Nuctch a book all the source code and test a few simple items)

    Multiline balloon tooltips on ListView items

    Description: The attached code demonstrates a technique you can use to create multiline balloon tooltips for ListView items.The code is based on the following simple idea. In the MouseMove event you ...

    QT5.5 Demo - Simple Tree Modek Example

    Simple models represent data as a table of items, and allow views to access this data via an index-based system. More generally, models can be used to represent data in the form of a tree structure ...

    simple_items_list_example:express + mongo 应用示例

    请首先运行npm i来安装依赖项(其中一些是自动生成的,现在不需要)。 我已经在 DB 设置了一些数据。 您可以随心所欲地更改它。 但我没有附加数据库生成的脚本。 有几个同名的产品 - 这不是错误。...

    simple-scroll:响应式水平滚动

    $('.your-items').scroll(options)在你想要的样式的项目列表上$('.your-items').scroll(options) options = { speed: 400 // scroll speed in ms } ###演示: ###作者: 瑞安·沃尔特斯

    simple-pagination:仅提供简单的分页逻辑

    简单分页 仅提供简单的分页逻辑安装npm install --save simple-pagination原料药/* * Returns pagination result * * @param { number } totalCount - Number of items that will be paginated * @param { number } ...

    Essential SNMP 2nd Edition

    The Simple Network Management Protocol (SNMP) is an Internet-standard proto- col for managing devices on IP networks. Many kinds of devices support SNMP, including routers, switches, servers, ...

    PIM_接口_PIM_assignment_java上机实验_pimcmd_

    编写基于命令行形式的个人信息管理(PIMCmd)程序,要求如下:This assignment involves the creation of simple Personal Information Management (个人信息管理 ) system that can deal with 4 kinds of items: ...

    Homework 7_Knowledge items of C++_(part 2).zip

    Each task requires writing some simple code to reflect your understanding of some knowledge of C++. II. Tasks of the homework Read the files in the folder Code of this assignment on Moodle. The ...

    test-simple-rest

    休息服务器测试 ####我如何运行它?... ... * 启用SQLite 扩展和Composer composer install sqlite3 app.db < resources> http://localhost:8000/api/v1/items/save *this should be post but I make it PUT r

    simple-chip:Web组件芯片输入

    <单芯片> Web组件芯片输入安装Polymer-CLI 首先,请确保您已安装和npm(与打包在一起)。 运行npm install来安装元素的依赖项,然后... addChips ( [ 'array' , 'of' , [ 'nested' , 'items' ] ] ) ;// orlet newCh

    右键管理 Fast Explorer 3 [免费版]

    Fast Explorer 3.1.11.430 绿色英文版[管理上下文右键菜单 ] 一个管理文件或目录的上下文右键菜单的工具,以资源... fast explorer is a very simple way to make your often used file actions quickly accessible!

    LMD.StoragePack.v1.06.30.For.Delphi

    strings or more complex types like components or TPersistent descendants (whereby even nested properties are supported) Supports simple data compression for binary items Allows data encryption for ...

    simple-rotator:元素js脚本的简单旋转器(轮播)

    在您的项目上添加simple_rotator.js文件。 它需要jQuery 1.0+ $ ( '.carousel-items' ) . simpleRotator ( { unique_id : '12345' , item_class : ".single-item" , items_visible : 3 } ) ; } ) ; 其他...

    networth_simple

    networth_simple 入门 正在安装 在networth_simple文件夹下,导航到/ server,然后使用npm install安装软件包: cd server ...localhost:3000/items 数据保存在服务器端。 没有使用数据库(目前)。

    simple-rss:一个用于Ruby的简单,灵活,可扩展和开放的RSS和Atom阅读器。 它被设计为与标准RSS解析器向后兼容,但绝不会生成RSS。

    下载gem install simple-rss git clone :cardmagic / simple-rss.git用法该API与Ruby的标准RSS解析器类似: require 'rubygems'require 'simple-rss'require 'open-uri'rss = SimpleRSS.parse open('...

    Jquery-Simple-Show-More-Items:使用“查看更多”按钮折叠长内容

    jQuery:简单显示更多项目描述: *大家好!* :male_sign:‍:male_sign: :male_sign:‍:male_sign: 在这个项目中,我制作了一个简单的应用程序,该应用程序使用查看更多按钮折叠长内容。 希望您能喜欢!...

    Android代码-具有内置搜索选项的强大且可自定义的搜索对话框。

    search-dialog An awesome and customizable search dialog with built-in search ...if you just want to use the simple search dialog first you need to provide searchable items. to achieve this you should

Global site tag (gtag.js) - Google Analytics