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

This is a discussion on What should I return, if the pointer I'm trying to return doesn't exist? within the C++ Programming forums, part of the General Programming Boards category; 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> ...

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

    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,328
    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, 06: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, 09:33 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21