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是怎么简明的!

 

posted on 2018-06-17 19:32  王家恒  阅读(138)  评论(0编辑  收藏  举报

导航