首页 > 编程语言 > 详细

Effective C++ Item 9 Never call virtual functions during constrution or destruction

时间:2014-02-28 17:57:51      阅读:436      评论:0      收藏:0      [点我收藏+]

Because such calls would never go to a more derived class than that of currently executing construtor or destructor. In other word, it would call the version of base class rather than that of derived classes. For example, if you have a base transaction class to log the buy and sell transactions, you may code like this

class Transaction {
public:
    Transaction() {
        ...
        logTransaction();
    }
    virtual void logTransaction();
};
 
class BugTransaction() : public Transaction {
public:
    virtual void logTransaction() const {
        ...
    }
};

  

If you write code like that, you highly possible debug to hell to find out why your derived class method are never called.

Effective C++ Item 9 Never call virtual functions during constrution or destruction,布布扣,bubuko.com

Effective C++ Item 9 Never call virtual functions during constrution or destruction

原文:http://www.cnblogs.com/xinsheng/p/3572657.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!