zoukankan      html  css  js  c++  java
  • Effective C++ 笔记 —— Item 32: Make sure public inheritance models "is-a."

    Consider this code:

    class Rectangle 
    {
    public:
        virtual void setHeight(int newHeight);
        virtual void setWidth(int newWidth);
        virtual int height() const; // return current values
        virtual int width() const;
        // ...
    };
    
    void makeBigger(Rectangle& r) // function to increase r’s area
    {
        int oldHeight = r.height();
        r.setWidth(r.width() + 10); // add 10 to r's width
        assert(r.height() == oldHeight); // assert that r's height is unchanged
    }

    Clearly, the assertion should never fail. makeBigger only changes r's width. Its height is never modified. Now consider this code, which uses public inheritance to allow squares to be treated like rectangles:

    class Square: public Rectangle { /*...*/ };
    Square s;
    // ...
    
    assert(s.width() == s.height()); // this must be true for all squares by inheritance, s is-a Rectangle, so we can increase its area
    
    makeBigger(s); 
    
    assert(s.width() == s.height()); // this must still be true for all squares

    It's just as clear that this second assertion should also never fail. By definition, the width of a square is the same as its height. But now we have a problem. How can we reconcile the following assertions?

    • Before calling makeBigger, s's height is the same as its width;
    • Inside makeBigger, s's width is changed, but its height is not;
    • After returning from makeBigger, s’s height is again the same as its width. (Note that s is passed to makeBigger by reference, so makeBigger modifies s itself, not a copy of s.)

    public inheritance asserts that everything that applies to base class objects — everything! — also applies to derived class objects.

    Things to Remember:

    • Public inheritance means ”is-a.“ Everything that applies to base classes must also apply to derived classes, because every derived class object is a base class object.
  • 相关阅读:
    你知道RAID的初始化过程吗?
    Dynamic Disk Pool技术解析
    Ubuntu-16.04.6 安装 oracle 11.2.0.4 数据库database软件
    Ubuntu-16.04.6 安装 oracle 12.2.0.1 数据库database软件
    IDEA 快捷键
    Redis 常用操作命令
    甘特图
    PERT图
    现金贷
    线上信贷提供解决方案
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15261431.html
Copyright © 2011-2022 走看看