《高质量程序设计指南——C++/C》(第三版)最新修订

来源:互联网 发布:乐视2移动数据上不了网 编辑:程序博客网 时间:2024/04/30 13:22

http://blog.csdn.net/northwest_wolf/archive/2007/07/23/1702594.aspx

1.Page 7:倒数第13行,“啰里啰唆”印刷有问题;

2.Page 15: 1-2有些许印刷问题;

3.Page 56:“4.3 类型转换”上面一段末尾应从“关于复合。。。”处另起一行;

4.Page 58: 示例4-6,代码用下列代码替换:

class Base

{

private:

int m_a;

int m_b;

// 示例

Derived objD1;

Base objB1 = objD1;      // 见图4-3左图

Derived *pD1 = &objD1;

 

};

class Derived : public Base

{

int m_c

};

 

Base *pB1 = pD1;       // 见图4-3右图

 

5.Page 76:示例4-13上半部分最后一行“delete a[];”要右移4格对齐;下半部分最后一行左移4格对齐;

6.Page 81:示例4-20中的注释/*第三条。。。*/修改为/*第二条。。。*/

7.Page 83:在“最后我们举一个数值计算。。。。”一行前面插入“5数值计算。”;

8.Page 88[建议5-1]中的示例代码:MAXMAXPI应该垂直对齐,后面的注释也垂直对齐;

9.Page 91:“C++程序”一行中:右边的[方法二]和左边的[方法二]要水平对齐,调整后后面的[方法三][方法四]也要分别与左边的[方法三][方法四]水平对齐;

10.          Page 93:倒数第10行,将“定义和初始化每一个常量时,一次一个样。”修改为“定义和初始化每一个常量一次。”;

11.          Page 96: 示例6-1:将“T是形参”修改为“_T是形参”;

12.          Page 107:示例6-8中,后面的注释行要垂直对齐;

13.          Page 122:示例7-1中,所有“cout <<。。。”行中的“:0x”要垂直对齐;

14.          Page 144:示例8-5右半边“char ch; ”一行要缩进4格垂直对齐;

15.          Page 147:上方代码中最后一个“:8”中间插入一个空格;

16.          Page 181[建议10-5]中,把“/t”修改为‘/t’;

17.          Page 183: 示例10-6用下面的代码替换(主要是对齐问题):

 

/*

 *  函数头注释

 */

void Function(float x, float y, float z)

{

    ...

}

 

if (...)

{

    ...

    while (...)

    {

        ...

    } // end of while

    ...

} // end of if

 

18.          Page 193: 示例12-3中少写了一句话,所以用下面的代码替换(红色表示修改的地方):

typedef  unsigned char  BYTE;

template<unsigned int N  /*容量(字节数)*/>

class RingBuffer {

public:

    typedef size_t                       size_type;

    typedef GenericLocker<CriticalSection>  _BufferLocker;

    RingBuffer() : m_pushPos(0), m_popPos(0), m_count(0) {

assert(N > 0);

        m_pRingBuffer = new BYTE[N];

    }

    ~RingBuffer() { delete []m_pRingBuffer; }

                    RingBuffer(const RingBuffer<N>& copy) : m_popPos(0) {

                       assert(N > 0);

                       m_pRingBuffer = new BYTE[N];

                       size_type rearLen = N - copy.m_popPos;

                       if (rearLen >= copy.m_count) {

             ::memmove(m_pRingBuffer,

                         &copy.m_pRingBuffer[copy.m_popPos],

                         copy.m_count);

                       }

                       else {

            ::memmove(m_pRingBuffer,

                          &copy.m_pRingBuffer[copy.m_popPos],

                          rearLen);

               ::memmove(m_pRingBuffer + rearLen,

                         copy.m_pRingBuffer, copy.m_count - rearLen);

                       }

                        m_pushPos = m_count = copy.m_count;

                    }

                    RingBuffer& operator=(const RingBuffer<N>& other) {

                       if (this != &other) {

                          _BufferLocker guard(m_mutex);

                          RingBuffer<N> temp(other);        // invoke copy constructor

                          _Swap(temp);                    // this->_Swap();

                       }

                       return (*this);

                    }

    bool is_full() const {

        _BufferLocker guard(m_mutex);

        return (m_count == N);

    }

    bool is_empty() const {

        _BufferLocker guard(m_mutex);

        return (m_count == 0);

    }

    size_type size() const {

        _BufferLocker guard(m_mutex);

        return m_count;

    }

    size_type capacity() const { return N; }

    size_type push(const BYTE *data, size_type length) {

        _BufferLocker guard(m_mutex);

        assert(data != NULL);

        if (length == 0 || length > (N - m_count))

            return 0;

        size_type rearLen = N - m_pushPos;              // 尾部剩余空间

        if (length <= rearLen) {

            ::memmove(&m_pRingBuffer[m_pushPos], data, length);

            m_pushPos += length;

            m_pushPos %= N;                          // 调整新的push位置

        }else{

            ::memmove(&m_pRingBuffer[m_pushPos], data, rearLen);

            ::memmove(m_pRingBuffer, data + rearLen, length - rearLen);

            m_pushPos = length - rearLen;                // 调整新的push位置

            }

        m_count += length;

        return (length);

    }

    size_type pop(BYTE *buf, size_type length) {

        _BufferLocker guard(m_mutex);

        assert(buf != NULL);

        if (length == 0 || length > m_count)

            return 0;

        size_type rearLen = N - m_popPos;                // 尾部剩余数据

        if (length <= rearLen) {

            ::memmove(buf, &m_pRingBuffer[m_popPos], length);

            m_popPos += length;

            m_popPos %= N;                             // 调整新的pop位置

        }else {

            ::memmove(buf, &m_pRingBuffer[m_popPos], rearLen);

            ::memmove(buf + rearLen, m_pRingBuffer, length - rearLen);

            m_popPos = length - rearLen;                  // 调整新的pop位置

            }

        m_count -= length;

        return (length);

    }

    void clear() {

        _BufferLocker guard(m_mutex);

        m_pushPos = 0, m_popPos  = 0, m_count = 0;

    }

private:

    RingBuffer(const RingBuffer<N>&);

    void operator=(const RingBuffer<N>&);

private:

    BYTE          *m_pRingBuffer; // buffer

    size_type         m_pushPos;               // 新的push位置:pushPos=(popPos+count)% N

    size_type          m_popPos;             // 新的pop位置

    size_type         m_count;               // 有效字节数

    CriticalSection    m_mutex;

};

 

19.          Page 196:示例12-6中,三个class所在行对齐有问题,后面两个class行要左移4空格垂直对齐;

20.          Page 199:图12-1用下图替换(原图没有使用斜体):

 

21.          Page 200:示例12-11中,class Eye的“public:”所在行左移4格垂直对齐,同时void行向右缩进4格;同时正数第13行“。。。返回其中任意一个接口指针”修改为“。。。返回其中任意一个接口指针”;

22.          Page 201:上方class Headprivate部分以m_打头的部分要垂直对齐;

23.          Page 207: 倒数第1213行,请把“//3)”和“// OK!”垂直对齐;

24.          Page 210:示例12-19,最后一行向左移4格垂直对齐;

25.          Page 233:上方“class B”行向左移4格垂直对齐;

26.          Page 270:把示例15-3中第8行到第13行分别左移4格;

27.          Page 275:把[建议15-1]中的“比如反跟踪”修改为“(比如反跟踪)”;

28.          Page 297:示例16-10:最后的“catch”一行向左移4格对齐;

29.          Page 301:正数第6行末尾句号改为冒号;

30.          Page 309: 把“必须派生自类似于接口IUnknown类”修改为“必须派生自类似于接口IUnknown类”;同时将图16-1用下图替换:

 

31.          Page 310:示例16-22中间“strVect.insert(…);”、“}”和“”所在的三行都分别右缩进4格;

32.          Page 310:将“而不是真正数据”修改为“而不是真正的数据”;将“指针元素指向对象”修改为“指针元素所指对象”;

33.          Page 317:最后一行末尾句号改为冒号;

34.          Page 320:示例16-30,把“void func()”和“{”两行左移4格对齐;

35.          Page 320:示例16-31中,把最后一行右缩进4格对齐;

36.          Page 333:正数第15行末尾句号改为冒号;

37.          Page 341:倒数第9行末尾句号改为冒号;

38.          Page 343:示例17-7存在一个小问题,现用下面的代码替换(红色是修改过的地方):

template<typename T  /*元素类型*/ , unsigned int N /*容量*/ >

class CyclicQueue {

public:

    typedef T                    value_type;

    typedef size_t                 size_type;

    typedef T&                   reference;

    typedef const T&              const_reference;

 

    CyclicQueue() : m_popPos(0), m_count(0) {

        assert(N > 0);

        m_beginPtr = (T*)(::operator new(sizeof(T) * N));  // 分配原始空间

        }

    ~CyclicQueue() {

        _Clear();                                              // this->_Clear();

        ::operator delete((void*)m_beginPtr);

        }

    CyclicQueue(const CyclicQueue<T, N>& copy) : m_popPos(0), m_count(0) {

        assert(N > 0);

        m_beginPtr = (T*)(::operator new(sizeof(T) * N));            // 分配原始空间

        size_t copyPos = copy.m_popPos;

        for (size_type idx = 0; idx < copy.m_count; ++idx) {

            _Copy(idx, copy.m_beginPtr[copyPos]);        // this->_Copy();

            ++copyPos; copyPos %= N; ++m_count;

            }

        }

    CyclicQueue& operator=(const CyclicQueue<T, N>& other) {

        if (this != &other) {                              // 检查自赋值

             CyclicQueue<T, N> temp(other);              // 调用拷贝构造函数

             _Swap(temp);                             //  this->_Swap();

            }

        return (*this);

        }

    bool is_empty() const { return (m_count == 0); }

    bool is_full() const { return (m_count == N); }

    value_type front() {

        assert(m_count != 0);

        return (m_beginPtr[m_popPos]);

        }

    value_type front() const {

        assert(m_count != 0);

        return (m_beginPtr[m_popPos]);

        }

    value_type back() {

return _Back();                                  // this->_Back();

        }

    value_type back() const {

return _Back();                                  // this->_Back();

        }

    bool push(const_reference data = T()) {

        if (m_count < N) {                                                          // 不满!

            size_type pushPos = (m_popPos + m_count) % N;

            _Copy(pushPos, data);                                      // this->_Copy();

            ++m_count;

            return true;

            }

        return false;

        }

    bool pop(reference data) {

        if (m_count > 0) {                                                           // 不空!

            data = m_beginPtr[m_popPos];                       // operator=

            _Destroy(m_popPos);                               // this->_Destroy();

            --m_count; ++m_popPos; m_popPos %= N;     // 新的pop位置

            return true;

            }

        return false;

        }

    size_type size() const { return m_count; }

    size_type capacity() const { return N; }

    void clear() { _Clear(); }                                     // this->_Clear();

    void swap(CyclicQueue<T, N>& other) {

        _Swap(other);                                   // this->_Swap();

        }

private:

    void _Clear() {

        for (; m_count > 0; --m_count) {

            _Destroy(m_popPos);                                         // this->_Destroy();

            ++m_popPos; m_popPos %= N;

            }

        m_popPos = 0;

    }   

    void _Destroy(size_type idx) {

        assert(idx < N);

        T *pTemp = (m_beginPtr + idx);

        pTemp->~T();                                      // 调用析构函数销毁元素对象

    }

    void _Copy(size_type idx, const_reference data) {

        assert(idx < N);

        T *pTemp = (m_beginPtr + idx);

        new ((void*)pTemp) T(data);  // 调用placement new拷贝构造函数复制对象

    }

void _Swap(CyclicQueue<T, N>& other) {

        std::swap(m_beginPtr, other.m_beginPtr);

        std::swap(m_popPos, other.m_popPos);

        std::swap(m_count, other.m_count);

}

value_type _Back() const {

        assert(m_count != 0);

        size_type pushPos = (m_popPos + m_count) % N;

        if (pushPos == 0)

            return (*(m_beginPtr + N - 1));

        return (m_beginPtr[pushPos - 1]);

}

 

    value_type        *m_beginPtr;     // 队列存储空间起始位置

    size_type          m_popPos;        // 下次pop位置

    size_type          m_count;          // 有效元素个数

};

 

39.          Page 348:将“顾名思义”这一段第二行的“输出功能”改为“输入/输出功能”;

40.          Page 357:图17-12存在一些问题,不知道是印刷问题还是绘图有问题,不管如何请用下图替换:

41.          附录E:“POD”所在行右边中文解释没有对齐,只需在末尾换行即可;

 

(完)

 
原创粉丝点击