returning object in a map

This is a discussion on returning object in a map within the C++ Programming forums, part of the General Programming Boards category; Hello I have a class with a map where I store some data: Code: class someobject { }; class someclass ...

  1. #1
    l2u
    l2u is offline
    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
    21,884
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    l2u
    l2u is offline
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    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,338
    >> 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, 10:15 AM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 08:11 AM

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