《C++ Primer Plus》第十二章学习笔记

 

132:静态类成员有一个特点:无论创建了多少对象,程序都只创建一个静态类变量副本。并且不能在类声明中初始化静态成员变量。

 

133:在构造函数中使用new来分配内存时,必须在相应的析构函数中使用delete来释放内存。

 

134:Implicit Member Functions: (P383 very important!!!) C++ automatically provides the following member functions:

           • A default constructor if you define no constructors

           • A copy constructor if you don’t define one

           • An assignment operator if you don’t define one

           • A default destructor if you don’t define one

           • An address operator if you don’t define one

 

135:关于复制构造函数:

           一、何时调用复制构造函数:

           1、className object1 (object2);

           2、className object1 = object2;

           3、className object1 = className(object2);

           4、className* p = new className(object2);

 

           二、复制构造函数的功能:

           The default copy constructor performs a member-by-member copy of the nonstatic members(memberwise copying, also

           sometimes called shallow copying).         

 

           如果类中包含了使用new初始化的指针成员,应当定义一个复制构造函数,以复制指向的数据,而不是仅复制指针,这被称为

           深度复制(deep copying)。

 

136:关于赋值操作符:

           一、何时使用赋值操作符:将已有的对象赋给另一个对象时,将使用重载的赋值操作符。

 

          二、赋值操作符的功能:与复制构造函数相似,赋值操作符的隐式实现也对成员进行逐个复制。

 

          一些注意点:

           1、Because the target object may already refer to previously allocated data, the function should use delete [] to free former

           obligations.

          2、The function should protect against assigning an object to itself; otherwise, the freeing of memory described previously could

           erase the object’s contents before they are reassigned.

          3、The function returns a reference to the invoking object, so that the function can emulate the way ordinary assignment for

          built-in types can be chained.

 

137:静态类成员函数:

          1、不能通过对象调用静态成员函数,且没有this指针;

          2、不与特定的对象相关联,因此只能使用静态数据成员。

 

138:Things to Remember When Using new in Constructors:

          1、If you use new to initialize a pointer member in a constructor, you should use delete in the destructor.

          2、The uses of new and delete should be compatible. You should pair new with delete and new [] with delete [].

          3、If there are multiple constructors, all should use new the same way—either all with brackets or all without brackets. There’s

                 only one destructor, so all constructors have to be compatible to that destructor. However, it is permissible to initialize a

                 pointer with new in one constructor and with the null pointer (NULL or 0) in another constructor because it’s okay to apply the

                 delete operation (with or without brackets) to the null pointer.

          4、You should define a copy constructor that initializes one object to another by doing deep copying. Typically, the constructor

                 should emulate the following example:

          String::String(const String & st)

          {

                     num_strings++;               // handle static member update if necessary

                    len = st.len;                       // same length

                     str = new char [len + 1]; // allot space

                     std::strcpy(str, st.str);   // copy string to new location

          }

          In particular, the copy constructor should allocate space to hold the copied data, and it should copy the data, not just the address

          of the data. Also, it should update any static class members whose value would be affected by the process.

          5、You should define an assignment operator that copies one object to another by doing deep copying. Typically, the class

          method should emulate the following example:

          String & String::operator=(const String & st)

          {

                     if (this == &st)                  // object assigned to itself

                     return *this;                       // all done

                     delete [] str;                       // free old string

                     len = st.len;

                     str = new char [len + 1];  // get space for new string

                     std::strcpy(str, st.str);   // copy the string

                     return *this;                       // return reference to invoking object

          }

          In particular, the method should check for self-assignment; it should free memory formerly pointed to by the member pointer; it

          should copy the data, not just the address of the data; and it should return a reference to the invoking object.

 

139:重载<<时,必须返回ostream的引用,若返回ostream,将要求调用ostream类的复制构造函数,而ostream没有公有的复制构

           造函数。

 

140:NULL是一个被定义为0的符号常量。

 

141:P409实现了队列 。

 

142:对于const数据成员 和 被声明为引用的类成员,能且只能通过初始化列表进行初始化,而不是被赋值。(初始化列表只适合

           构造函数)

 

 

 

书中的错误:

P379 程序第二个include后面的string少了个i;

P382 倒数第7行的>>应改为<<;

P390 标题2下面的第三行字符串比较函数少了一个“s”;

P390 第一个重载<的函数中 “>”应改为”<”;

P390 往下数五行的”>”应改为”<”;

P390 重载>的函数中” st2.str<st1.str” 应改为 ” st2 < st1”;

P391 倒数17行的”[0]”应改为”(0)”;

P392 第八行的”cout”应改为”cin”;

P393 程序12.5上面的枚举赋值,90应改为80;

P394 默认构造函数中的len应赋为0;

P400 第三行的第二个“s2”改为“s1”; 

 

 

 

 

 

posted on 2012-01-27 12:59  zyearn  阅读(171)  评论(0编辑  收藏  举报