轻松一点的东西
> 同一个 class:
>
> class array {
> public:
> int a[2];
> array update() {
> a[0]=a[0]*3;
> a[1]=a[1]*3;
> return(*this); }
> };
>
> 两个不同的 main:
>
> main()
> {
> array n;
> n.a[0]=1; n.a[1]=2;
> n.update();
> n.a[0]=0;
> cout << n.a[0] << endl << n.a[1] << endl;
> }
>
> main()
> {
> array n;
> n.a[0]=1; n.a[1]=2;
> (n.update()).a[0]=0; // 两行合并成一行
> cout << n.a[0] << endl << n.a[1] << endl;
> }
太多的写作毛病:
a. Effctive C++:
条款20:避免将 data members 放在公开介面中
Item 20: Avoid data members in the public interface.
b. Effctive C++:
条款22:尽量使用 pass-by-reference(传址),少用 pass-by-value(传值)
Item 22: Prefer pass-by-reference to pass-by-value.
c. Effctive C++:
条款29:避免传回内部资料的 handles
Item 29: Avoid returning "handles" to internal data.
d. More Effctive C++:
条款19:了解暂时性物件的来源
Item 19: Understand the origin of temporary objects
e. More Effctive C++:
条款22:考虑以复合运算子 op= 取代单一运算子 op
Item 22: Consider using op= instead of stand-alone op