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.
  • 相关阅读:
    poj2356
    poj2021
    网络直销:贯通式电子商务营销
    网络营销趋同化时代到来
    一个睡五分钟等于六个钟头的方法
    张胜(帮别人名字作诗)
    我在阳光下向你表白
    李建逸(帮别人名字作诗)
    夜深,你的手机为谁而开
    杨英(帮别人名字作诗)
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15261431.html
Copyright © 2011-2022 走看看