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.

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

上一篇:MATLAB 不同维度矩阵加减乘除


下一篇:Boob炸弹拆除IDA版