《高质量程序设计指南——C++/C》第三版勘误

来源:互联网 发布:县域经济金融数据库 编辑:程序博客网 时间:2024/05/21 07:06
《高质量程序设计指南——C++/C》第三版勘误

1.Page IX1.5 “关于软件开发。。。”存在印刷问题;

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

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

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 81:示例4-20中的注释/*第三条。。。*/修改为/*第二条。。。*/

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

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

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

9.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; }

    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;

};

 

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

11.          Page 309: 把“必须派生自类似于接口IUnknown类”修改为“必须派生自类似于接口IUnknown类”;

12.          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) {

        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;          // 有效元素个数

};

 

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

14.          Page 357:图17-12用图替换:

 

 

韩永泉

2007-6-18

 
原创粉丝点击