C++ 使用成员初始化列表的一个小坑

注意在成员列表中初始化的顺序并不是列表顺序 而是:

  • 在类中声明的顺序!
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;
	}
}


初始化顺序是由

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;

这里决定的。。。

在成员初始化列表中有前后顺序依赖的时候一定要注意!

posted @ 2018-04-24 17:27  joeylee97  阅读(414)  评论(0编辑  收藏  举报