Managing C++ Objects: 管理C++对象 —— 一些建议准则
原文链接: Managing C++ Objects
Here are some guidelines I have found useful for writing C++ classes. There are many good books on the subject, but they have not been sufficient to keep me out of trouble.
(有很多书都在讨论这些主题,但是都没能让我真正搞明白)
The first time I returned to writing C++ after a year of writing Java, I was appalled at how much my design was constrained by managing the lifetime of objects.
( 当我写了很久Java以后再回头写C++才吃惊的发现,写C++是的‘管理对象的生命周期’ 让我耗费了太多的精力、受到了很多约束 )
When C++ classes share objects, then they must negotiate who owns the object. Garbage collection is not available, and smart pointers often fall short.
(C++里传递对象时,必须关注对象的所有权归于谁。没有垃圾收集,智能指针也捉襟见肘)
一句话:当你被C++里繁琐、细枝末节的语法设计搞糊涂时,多想想用Java的话会怎么写,为什么Java写起来那样简明。(C++是可以模拟Java/c#风格的)
1. 构造函数要简单。
如果几个构造函数对于某些member field设置的初始值是一样的,可以将这个抽出来成 init()方法,在每个构造函数里调一下。
2. 有几个基本的接口要配上
① 拷贝构造函数; ② 重载赋值运算符= ③ (如果可能被继承) virtual ~T() 析构函数 + dispose() 资源释放函数
3. 成员变量不要用引用&类型
因为引用成员变量必须在构造函数时进行对象绑定,而且以后不能再更换。真的没必要!
4. Optional Ownership 可选的所有权
5. 参数不要用指针形式
Pass all objects to class methods and constructors as references.(参数全部用引用&传递) There is absolutely no advantage to passing objects as pointers. (用指针传递对象没有任何优势)This rule is equally valid whether the objects are const or not.
I've already recommended that all class members be saved as pointers. You can easily take the address of an argument reference (with an ampersand) and assign it to your member pointer.
( 成员变量用的是T* pt, 参数形式是 T& t,那么只要简单的多写句 pt = &t 就行了)
If an object is passed to a constructor or initialization method, the user can expect the class to hang onto it. If a method saves an object from an argument, choose an appropriate name, like setColor(Color&) or addInterpolator(Interpolator&).
The worst excuse for using a pointer as an argument is that you want to give it a default value of null (0). You still have to document what a null object is supposed to mean. Worse, the user may overlook that the argument exists or is optional. Declare a separate method that lacks the extra argument. The effort is negligible.
6. return 对象的形式
One can always return objects from class methods by reference, either const or non-const. A user can take the address of the reference, if necessary, to save the object.
But there are no drawbacks to returning objects always as pointers. Consistency is preferable, and most API's return pointers. (用指针也没什么坏处)
(总之,保持一致性。大多数api也是返回对象的指针的)
If you return an object allocated on the heap (with a new), then be clear who has ownership of the object--your class, the recipient, or a third party.
Think about whether you are breaking encapsulation of member data in a way that will prevent modification later.
Never return a reference to a class member allocated on the stack in the header file. If your class replaces the value, then the user may be left with an invalid reference, even though your object still exists. (Other reasons: Your class will never be able to remove the object as a member. A user may manipulate the logic of your class in unexpected ways.)
A method should modify an object constructed by the user by accepting it as a non-const reference. Returning the same object would be redundant and confusing.
7. 多模仿 Java
当C++里太多细节考虑弄得心烦意乱时, 学学Java是怎么简明的!
Managing C++ Objects: 管理C++对象 —— 一些建议准则的更多相关文章
- Extension Objects(扩展对象)
设计模式之美:Extension Objects(扩展对象) 索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):使用示例结构实现 Extension Objects. 实现方 ...
- CreateWindowEx failed (当前程序已使用了 Window 管理器对象的系统允许的所有句柄。)
我在QT图形场景视图中通过QGraphicsProxyWidget添加代理Widget(实现添加基本的QT Widget,如按钮.复选框.日期时间控件等),当数量超过3500左右的时候,QT应用程序直 ...
- python类与对象-如何创建可管理的对象属性
如何创建可管理的对象属性 问题举例 在面向对象编程中, 我们把方法看作对象的接口, 直接访问对象的属性可能是不安全的,或设计上不够灵活. 但是使用调用方法在形式上不如访问属性简洁. circle.ge ...
- admin.ModelAdmin 后台管理关联对象,某个字段怎么显示值
admin.ModelAdmin 后台管理关联对象,某个字段如何显示值?对象 WxpAccount: accountName = ... 对象 AccountMenu: ...
- 使用Executor管理Thread对象详解
java SE5的java.util.concurrent包中的执行器(Executor)是管理Thread对象的优选方法.使用Executor管理Thread对象可以简化并发编程. Executor ...
- 爬虫学习(五)——使用handler管理器对象进行数据爬取的步骤
# 使用管理器对象进行爬取数据的步骤 import urllib.requesturl = "https://www.baidu.com/"# 创建handler的管理器对象han ...
- Docker笔记(二):Docker管理的对象
原文地址:http://blog.jboost.cn/2019/07/14/docker-2.html 在Docker笔记(一):什么是Docker中,我们提到了Docker管理的对象包含镜像.容器. ...
- 开源基于lua gc管理c++对象的cocos2dx lua绑定方案
cocos2dx目前lua对应的c++对象的生命周期管理,是基于c++析构函数的,也就是生命周期可能存在不一致,比如c++对象已经释放,而lua对象还存在,如果这时候再使用,会有宕机的风险,为此我开发 ...
- OC内存管理--zombie对象
当我们对于内存进行手动管理的时候,会出现两种错误:一种是野指针错误,一种则为内存泄露.这两点也是我们去管理内存时最终要解决的问题. 内存泄漏是指:不在使用的对象,一直保留在内存中未被销毁,一直占有着内 ...
随机推荐
- PHP将图片转base64格式函数
base64的好处是什么?今天在跟小伙伴讨论这个问题,要是全站用Php把图片转为base64行不行? 1. 提升性能: 网页上的每一个图片,都是需要消耗一个 http 请求下载而来的, 图片的下载始终 ...
- yoast breadcrumb面包屑导航修改去掉product
前面我们创建了wordpress添加post_type自定义文章类型和调用自定义post_type文章,现在yoast 面包屑导航出现home >product >分类1,想要把produ ...
- Linux 命令 mv
mv 命令 --no-target-directory 参数确保对目录进行重命名而不是移动 https://www.gnu.org/software/coreutils/manual/html_nod ...
- ESA2GJK1DH1K基础篇: STM32+GPRS(AT指令版)实现MQTT源码讲解(支持Air202,SIM800)
前言 注: 本程序发送心跳包,发送温湿度,返回控制数据这三个发送是单独的,有可能凑到一起发. 由于本身程序就是复杂性的程序,所以这节程序没有使用中断发送,没有使用环形队列发送,为了避免多条消息可能凑到 ...
- <虚树+树型DP> HNOI2014世界树
<虚树+树型DP> HNOI2014世界树 #include <iostream> #include <cstdio> #include <cstring&g ...
- 数据结构——单链表(singly linked list)
/* singlyLinkedList.c */ /* 单链表 */ /* 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素. */ #include <stdio ...
- postman使用--Monitor
前戏 现在我们已经能完成接口的批量执行,添加断言,数据驱动,设置变量等等方法.但是有一天,用户反应说我们的网站访问不了了.这时候,那帮程序猿查日志的查日志,看数据库的看数据库,找到原因在发布到线上已经 ...
- 论OIer的同构
定义一个输入集,指这样一个集合:由任意存在于物理世界的OI题目以及做这个题的时间与身体状态等各种元素组成的n元组组成的集合 OIer被定义为一个二元组:<"结果集",&quo ...
- 从$a_n=f(n)$的角度理解数列中的表达式$a_{n+1}=\frac{k}{a_n}$
函数周期性 前面我们学习过函数的周期性的给出方式: \(f(x+a)=f(x)\) \(\hspace{2cm}\) \(T=a\) \(f(x+a)=-f(x)\) \(\hspace{2cm}\) ...
- UID、PID、PPID是什么?
UID是用户ID,PID是进程ID,PPID是父进程ID. UID UID 用户身份证明(User Identification)的缩写.UID用户在注册后,系统会自动的给你一个UID的数值.意思就是 ...