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

时间:2014-02-28 17:57:51   收藏:0   阅读:436

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

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!