Thread: What should I return, if the pointer I'm trying to return doesn't exist?

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    What should I return, if the pointer I'm trying to return doesn't exist?

    I have this class, the object manager, it looks like this:

    Code:
    class Object_Manager
    {
    public:
    	Object_Manager(){}
    	~Object_Manager(){}
    
    	typedef boost::shared_ptr<Object> Raw_Object;
    	typedef std::map<std::string, Raw_Object> Objects;
    	typedef boost::weak_ptr<Object> Data_Observer;
    
    	Data_Observer Access_Object(std::string Filename)
    	{
    		Objects::iterator it = Managed_Objects.find(Filename);
    		// check if the manager exists in the map
    		if (it == Managed_Objects.end())
    		{
    			// if not throw an error
    			std::cout << "Object does not exist!" << std::endl;
    		}
    		else
    		{
    			// if so return a pointer to the manager
    			return Data_Observer(it->second);
    		}
    	}
    
    	int Add_Object(std::string Filename, Object * Obj)
    	{
    		Objects::iterator it = Managed_Objects.find(Filename);
    		if(it == Managed_Objects.end())
    		{
    			Managed_Objects.insert(std::make_pair(Filename, Obj));
    		}
    		else
    		{
    			std::cout << "Object already exists" << std::endl;
    		}
    
    	}
    
    	void Remove_Object(std::string Filename)
    	{
    		Objects::iterator it = Managed_Objects.find(Filename);
    		if(it == Managed_Objects.end())
    		{
    			std::cout << "Object does not exist, can't remove!" << std::endl;
    		}
    		else
    		{
    			Managed_Objects.erase(it);
    		}
    	}
    
    private:
    	friend class boost::serialization::access;
        template<class Archive>
    	void serialize(Archive & ar, const unsigned int version)
        {
            // serialize base class information
            ar & Managed_Objects;
        }
    	Objects Managed_Objects;
    };
    My question is, what should I return if the object doesn't exist in the manager, what can my utility class use to determine a different course of action in case this happens?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you return null?

    If it's very rarely supposed to happen then throwing an exception might also be appropriate.

    If it is truly never supposed to happen then an assert might be best. Given that its based on a filename I doubt this will be the case, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM