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

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.

原文地址:https://www.cnblogs.com/xinsheng/p/3572657.html