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.
  • 相关阅读:
    如何使用 ADO 将数据从 ADO 数据源传输到 Excel
    C#2.0中的可空类型Nullable
    2007年你必须学习的10项.NET技术
    日期正则表达式
    只允许n个实例进行
    MFC程序隐藏任务栏图标的三种方法
    Qt4小技巧——将button布局在QToolbar的右边
    QT学习随笔20120813
    只允许一个实例,允许n个实例
    折腾的DVCS
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15261431.html
Copyright © 2011-2022 走看看