C++ uses a small pit of member initialization list

Note that the order of initialization in the member list is not list order but:

  • The order of declarations in the class!
EventLoop::EventLoop() :looping(false), quit(false),_tid(curThreadId()), poller(new Poller(this)){//, timerQueue(new TimerQueue(this)) {
    std::cout<<_tid<<std::endl;
    if (t_LoopInThisThread) {
//      LOG_FATAL << "Another EventLoop " << t_LoopInThisThread << " Exists in this Thread " << _tid;
    }
    else {
        t_LoopInThisThread = this;
    }
}

The initialization order is given by

private:
        typedef std::vector<Channel*> ChannelVec;
        std::unique_ptr<Poller> poller;
        std::unique_ptr<TimerQueue> timerQueue;
        Timestamp pollReturnTime;
        ChannelVec activeChannels; 
        ThreadId _tid;
        bool looping;
        bool quit;

decided here. . .

Be careful when there are sequential dependencies in the member initialization list!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324774775&siteId=291194637