I'm working on making a factory class but have run into a problem. I
think I'm trying to get the shared pointer out the wrong way.
std::map
On Sat, 07 Feb 2004 23:12:58 -0600, Joshua Little wrote
LoggerMap::iterator i = loggers.find(logger); if (!(*i).second.get()) {
If logger doesn't already exist in loggers, then i will be loggers.end(). Dereferencing end() is undefined. Use this for your conditional instead: if(i != loggers.end()) Or, if you want to gain speed, do this: boost::shared_ptr<Logger> LoggerFactory::getInstance(std::string& logger) { boost::shared_ptr<Logger>& TheLoggerRef = loggers[logger]; if(!TheLoggerRef.get()) TheLoggerRef.reset(new Logger()); return TheLoggerRef; } This version searches the map only once. find() doesn't add entries, but the array operator does. Todd
todd wrote:
On Sat, 07 Feb 2004 23:12:58 -0600, Joshua Little wrote
LoggerMap::iterator i = loggers.find(logger); if (!(*i).second.get()) {
If logger doesn't already exist in loggers, then i will be loggers.end(). Dereferencing end() is undefined. Use this for your conditional instead:
if(i != loggers.end())
Or, if you want to gain speed, do this:
boost::shared_ptr<Logger> LoggerFactory::getInstance(std::string& logger) { boost::shared_ptr<Logger>& TheLoggerRef = loggers[logger]; if(!TheLoggerRef.get()) TheLoggerRef.reset(new Logger()); return TheLoggerRef; }
This version searches the map only once.
find() doesn't add entries, but the array operator does.
Todd
Thanks Todd, now that I see what I did wrong I wonder why I didn't see it in the first place :) Joshua.
participants (2)
-
Joshua Little
-
todd