2009-11-25

来源:互联网 发布:博奥计价软件 编辑:程序博客网 时间:2024/05/21 17:21

 1、静态成员变量为const int 型时,可以直接在类体中初始化
   这个功能可以用在定义一个数组
   Integral const static Members Are Special
   Ordinarily, class static members, like ordinary data members, cannot be initialized
   in the class body. Instead, static data members are normally initialized when they
   are defined.

   One exception to this rule is that a const static data member of integral type can be
   initialized within the class body as long as the initializer is a constant expression:

     class Account {
     public:
         static double rate() { return interestRate; }
         static void rate(double);  // sets a new rate
     private:
         static const int period = 30; // interest posted every 30 days
         double daily_tbl[period]; // ok: period is constant expression
     };
  A const static data member of integral type initialized with a constant value is a
  constant expression. As such, it can be used where a constant expression is required,
  such as to specify the dimension for the array member daily_tbl.

2、使用容器时,先声明一个空容器,以后当需要时再往里面插入值效率更高(省了拷贝构造函数一步)
   As a general rule (Section 9.1.1, p. 307), unless you intend to use the default initial
   value of the container elements, it is more efficient to allocate an empty container and
   add elements as the values for those elements become known
3、拷贝、赋值、析构函数关系
   A useful rule of thumb is that if a class needs a destructor, it will also need the
   assignment operator and a copy constructor. This rule is often referred to as the Rule
   of Three, indicating that if you need a destructor, then you need all three copy-control members.
4、类中数据成员初始化顺序是按照它们在类中声明顺序,析构时则相反

原创粉丝点击