Thread: returning object in a map

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    returning object in a map

    Hello

    I have a class with a map where I store some data:
    Code:
    class someobject {
    };
    
    
    class someclass {
    public:
      someobject & get_object() {
      }
    
    private:
      std::map<std::string, someobject> m_map;
    };

    I'm not sure what would be the best way to implement get_object() function so that it would return reference to the object (if its in the map)..

    I know I could leave it like this and do throw catch if the map doesnt contain the object - but thats not the way I want to go..

    The next option is to use boost:: optional, but here I have a lot of 'crap' code for instance:

    boost:: optional<someobject&> oobject = ...;

    Does anyone know of any simpler/better way to implement it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I know I could leave it like this and do throw catch if the map doesnt contain the object - but thats not the way I want to go..
    It seems like the best way to me if you want to return a reference. The standard containers return iterators for their associative containers instead, so we cannot really use them as a reference. The sequential containers do return references to elements, but they leave it as undefined behaviour if the element does not actually exist (e.g., they just return a reference to an object that does not exist, like accessing an array out of bounds).

    Does anyone know of any simpler/better way to implement it?
    You could provide a getter/setter pair instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by laserlight View Post
    It seems like the best way to me if you want to return a reference.
    I agree with you, but the problem is I want to minimize the code, thats why I want this get_object() function but catch blocks wouldnt make any benefit in that case..

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then another approach could be to take a reference to an object to bind to and return a bool indicating success or failure.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Then another approach could be to take a reference to an object to bind to and return a bool indicating success or failure.

    That would have to be a pointer since using a reference wouldn't allow for the calling code to update the value actually stored in the map.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Object returning a bool
    By PJYelton in forum C++ Programming
    Replies: 15
    Last Post: 11-27-2002, 11:15 AM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM