• 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.

  • 相关阅读:
    换教室
    [国家集训队]礼物
    【模板】扩展卢卡斯(学习笔记)
    Desert King
    绿豆蛙的归宿
    Dropping tests
    [SDOI2013]随机数生成器
    佳佳的fib
    [USACO10OPEN]水滑梯Water Slides
    强大的XML
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3572657.html
Copyright © 2020-2023  润新知