构造函数调用构造函数是会问题的.
外层函数返回的内存, 与被调用的构造函数返回的内存并不是一个内存.
错误示例代码如下:
msg_log(const char *name, const char* thread_name, const char *cfg_file, int type = STREAM, int level = MSG_ERROR) : _name(name), _thread_name(thread_name), _level(level), _type(type), logfp(0), _set_process(false), _set_line(false), _set_file(false) { if(cfg_file) msg_log::cs_conf_file = cfg_file; init(type, level); pthread_mutex_init(&createfile_mutex,NULL); } msg_log(const char *name, const char *cfg_file, int type = STREAM, int level = MSG_ERROR) : _name(name), _level(level), _type(type), logfp(0), _set_process(false), _set_line(false), _set_file(false) { msg_log(name, "", cfg_file, type, level); }
太多年没写C++了, debug了好久, 才发现问题出在哪里.
参考: http://www.cnblogs.com/chio/archive/2007/10/20/931043.html
时间: 2024-10-11 06:44:15