zoukankan      html  css  js  c++  java
  • Effective C++ 笔记 —— Item 3: Use const whenever possible

    The following functions take the same parameter type:

    void f1(const Widget *pw); // f1 takes a pointer to a constant Widget object
    void f2(Widget const *pw); // so does f2

    Consider a class for representing a block of text:

    class TextBlock 
    {
    public:
        const char& operator[](std::size_t position) const // operator[] for const objects
        {
            return text[position];
        } 
        char& operator[](std::size_t position) // operator[] for non-const objects
        {
            return text[position];
        }
    private:
        std::string text;
    };
    
    TextBlock tb("Hello");
    std::cout << tb[0]; // calls non-const TextBlock::operator[]
    const TextBlock ctb("World");
    std::cout << ctb[0]; // calls const TextBlock::operator[]
    
    std::cout << tb[0]; // fine—reading a non-const TextBlock
    tb[0] = ’x’; // fine—writing a non-const TextBlock
    std::cout << ctb[0]; // fine—reading a const TextBlock
    ctb[0] = ’x’; // error!—writing a const TextBlock 

    There are two prevailing notions: bitwise constness (also known as physical constness) and logical constness.

    The bitwise const camp believes that a member function is const if and only if it doesn't modify any of the object's data members (excluding those that are static), i.e., if it doesn't modify any of the bits inside the object.

    Unfortunately, many member functions that don't act very const pass the bitwise test.

    class CTextBlock {
    public:
    
        char& operator[](std::size_t position) const // inappropriate (but bitwise const) declaration of operator[]
        {
            return pText[position];
        }
    
    private:
        char* pText;
    };
    
    const CTextBlock cctb("Hello"); // declare constant object
    char* pc = &cctb[0]; // call the const operator[] to get a pointer to cctb's data
    *pc = 'J'; // cctb now has the value "Jello"

    This leads to the notion of logical constness. Adherents to this philosophy — and you should be among them — argue that a const member function might modify some of the bits in the object on which it's invoked, but only in ways that clients cannot detect.

    class CTextBlock {
    public:
        std::size_t length() const;
    private:
        char* pText;
        mutable std::size_t textLength; // these data members may
        mutable bool lengthIsValid; // always be modified, even in
    }; // const member functions
    
    std::size_t CTextBlock::length() const
    {
        if (!lengthIsValid) {
            textLength = std::strlen(pText); // now fine
            lengthIsValid = true; // also fine
        }
        return textLength;
    }

    Having the non-const operator[] call the const version is a safe way to avoid code duplication, even though it requires a cast. 

    class TextBlock {
    public:
        //...
        const char& operator[](std::size_t position) const // same as before
        {
            //...
            //...
            //...
            return text[position];
        }
        char& operator[](std::size_t position) // now just calls const op[]
        {
            return const_cast<char&>(static_cast<const TextBlock&>(*this)[position]); // cast away const on op[]’s return type add const to *this’s type call const version of op[]
        }
        //...
    };
  • 相关阅读:
    mysql 查找数组格式的字符串中是否包含某个值
    假期总结
    shell循环结构解析:for/while/case
    ansible笔记(15):循环(二)with_items/with_list/with_together/with_flattened
    ansible笔记(14):循环(一)
    解决报错Failed to start LSB: Bring up/down networking:MAC地址导致
    实现ENSP模拟器与物理主机、虚拟机通信
    zabbix4.2配置监控华为路由器:基于ENSP模拟器
    Grafana展示zabbix监控数据
    zabbix4.2配置监控TCP连接状态
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/14917613.html
Copyright © 2011-2022 走看看