Effective C++ 条款总结 读书笔记(二)

Item Effective C++ 条款 item_20

Avoid data members in the public interface

This rule warns you if data members are present in the public interface. Instead of making data members public, you should use functions.

Reason for rule: Avoiding data members in the public interface provides more precise control over accessibility of data members, reduces confusion for callers, and enables functional abstraction (allowing the replacement of data members with computations without impacting users of the class). It also improves consistency, flexibility, and access-control.
Example

 /*
  * Item 20 - Avoid data members in the public interface
  */
 
 class A
 {
 public:
     int _idata2;                // Effective C++ 条款 item item 20 violation
     
     int ReadData1(void) { return _idata1; }
     
     void WriteData1(int ival) { _idata1 = ival; }
 
 private:
     int _idata1;
 };
 
 int main()
 {
     return 0;
 }

     

Output

 Avoid data members in the public
 interface
 Violation: Effective C++ item 20
 Public data members for class A:
 _idata2


Item Effective C++ 条款 item_22

Pass and return objects by reference instead of by value

This rule detects where an object has been passed by value instead of by reference.

Reason for rule: Passing and returning objects by reference is more efficient than passing by value because no new objects are being created and because it avoids the "slicing problem."

Exception: There are situations in which you cannot pass an object by reference (see Item Effective C++ 条款 item_23). There also may be a situation where the object is so small that it would be more efficient to pass by value instead of by reference.
Example

 /*
  * Item 22 - Pass and return objects by reference instead
  * of by value
  */
 
 
class A
 {
 public:
     A() {}
     A foo(const A a) // 2 Effective C++ 条款 item item 22 violations
     {                // passing variables into foo by value
       return a; // returning by value when it is not required
     }
 };
 
 int main()
 {
     return 0;
 }


Output

 Pass objects by reference instead of
 by value
 Violation: Effective C++ item 22
 Parameter a of function foo passed by value
 
 Return objects by reference instead of by
 value
 Violation: Effective C++ item 22
 Function foo returns object by value

Item Effective C++ 条款 item_23


Don't try to return a reference when you must return an object

This rule detects when you return a reference where you are supposed to return an object.

Reason for rule: Returning a reference instead of an object will result in corrupt data or a memory leak.
Example

 
 /*
  * Item 23 - Don't try to return a reference when you must
  * return an object.
  */
 
 class A
 {
 public:
     A(int num = 0, int den = 1) : _num(num), _den(den) {}
     int numerator() const { return _num; }
     int denominator() const { return _den; }
     
 private:
     int _num;
     int _den;
 };
 
 // Effective C++ 条款 item item 23 violation
 A& operator*(const A& lhs, const A& rhs)
 {
     A temp(lhs.numerator() * rhs.numerator(),
                   lhs.denominator() * rhs.denominator());
     return temp;
 }
 
 int main()
 {
     return 0;
 }


Output

 Don't try to return a reference when you
 must return an object
 Possible severe violation: Effective C++ item 23
 Operator * should return an object


Item Effective C++ 条款 item_25

Avoid overloading on a pointer and a numerical type

This rule detects when you are overloading on a pointer and a numerical type.  

Reason for rule: Calling with an argument of zero will invoke the numerical type even though it is intuitively ambiguous.
Example

 /*
  * Item 25 - Avoid overloading on a pointer and a numerica
  * type.
  */
 
 class A
 {
 public:
     int func(char *ch)
     {
        char c=*ch;
        return 0;
     }
     int func(int i)           // Effective C++ 条款 item item 25 violation
     {
         int var=i;
         return 0;
     }
 };
 
 int main()
 {
     return 0;
 }


Output

 Avoid overloading on a pointer and a
 numerical type
 Violation: Effective C++ item 25
 
 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值