您的位置:首页 > 编程语言 > C语言/C++

【ThinkingInC++】51、最好还是把定义放到外面,用inline来实现内联,而不是在类里面定义默认为内联

2014-09-16 19:05 507 查看
/**
* 书本:【ThinkingInC++】
* 功能:当项目比较大的时候,最好还是把定义放到外面,用inline来实现内联,而不是在类里面定义默认为内联
* 时间:2014年9月16日19:06:25
* 作者:cutter_point
*/

class Rectangle
{
    int width, height;
public:
    Rectangle(int w=0, int h=0);
    int getWidth() const;
    void setWidth(int w);
    int getHeight() const;
    void setHeight(int h);
};

inline Rectangle::Rectangle(int w, int h) : width(w), height(h) {}

inline int Rectangle::getWidth() const {return width;}

inline void Rectangle::setWidth(int w) {width=w;}

inline int Rectangle::getHeight() const {return height;}

inline void Rectangle::setHeight(int h) {height=h;}

int main()
{
    Rectangle r(19, 47);
    int iHeight=r.getHeight();
    r.setHeight(r.getWidth());
    r.setWidth(iHeight);

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: