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.

posted @ 2014-02-27 23:51  SangS  阅读(363)  评论(0编辑  收藏  举报