CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: How to create a function for storing object in map?

  1. #1
    Join Date
    Jul 2001
    Posts
    27

    Question How to create a function for storing object in map?

    I want to create a function which store object in map but each time i like to send different map and object.
    Please help me creating this type of function.

    Here is my dummy code

    Code:
    class Account
    {
    public:
        void Create(map, object)
        {
            // I want to store object in coming map
        }
    };
    
    class Saving : public Account
    {
        // no create method here
    };
    
    class Current : public Account
    {
        // no create method here
    };
    
    class Bank
    {
        Saving sb;
        Current cur;
    
        map<int, Saving> sbMap;
        map<int, Current> curMap;
    
        // Here i want to send map and object different every time
    
        // 1st time Saving class map and object
        sb.Create(sbMap, sb);
    
        // 2nd time Current class map and object
        cur.Create(curMap, cur);
    };

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,145

    Re: How to create a function for storing object in map?

    All you need is the correct definition of your Create method
    Code:
    void Create(std::map& map, const Account& object) const
    This assumes that Create does not modify the object passed into it and does not modify any other data members, thus the const Account& and the definition of the method as const.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: How to create a function for storing object in map?

    Quote Originally Posted by Richard.J
    All you need is the correct definition of your Create method
    Code:
    void Create(std::map& map, const Account& object) const
    I don't think that will work though: std::map is a template, and possible arguments include a map<int, Saving> object and a map<int, Current> object.

    It also looks like Create should not be a non-static member function. Maybe it should be a non-member non-friend function template:
    Code:
    template <typename MapType>
    void Create(MapType& map, const Account& object)
    {
        // store object in coming map
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jul 2001
    Posts
    27

    Re: How to create a function for storing object in map?

    Thank you Richard.J and laserlight but both idea is not working.
    template idea is working some what but it showing following errors:

    no matching function for call to 'Saving::create(Bank::bankMap*, Saving*)'
    template<class MapType> void Account::create(MapType&, const Account&)
    template argument deduction/substitution failed:
    cannot convert '& sb' (type 'Saving*') to type 'const Account&'

    If you find any answer please let me know.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,193

    Re: How to create a function for storing object in map?

    Please post your code. As an example, this below compiles OK
    Code:
    #include <map>
    using namespace std;
    
    class Account
    {
    	private :
    		static int ind;
    
    public:
    	template<typename M, typename O>
    	void Create(M& map, const O& object)
    	{
    		// I want to store object in coming map
    		map[ind++] = object;
    	}
    };
    
    class Saving : public Account
    {
    	// no create method here
    };
    
    class Current : public Account
    {
    	// no create method here
    };
    
    class Bank
    {
    
    	Saving sb;
    	Current cur;
    
    	map<int, Saving> sbMap;
    	map<int, Current> curMap;
    
    	void Create()
    	{
    		// Here i want to send map and object different every time
    
    		// 1st time Saving class map and object
    		sb.Create(sbMap, sb);
    
    		// 2nd time Current class map and object
    		cur.Create(curMap, cur);
    	}
    };
    
    int Account::ind = 0;
    
    int main()
    {
    	Bank b;
    }
    note the the index into the maps is common across all maps in this example.
    Last edited by 2kaud; January 21st, 2016 at 05:23 AM. Reason: See Post #7
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

  6. #6
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,145

    Re: How to create a function for storing object in map?

    A common index to be used in different kinds of maps seems odd to me. In addition, your Create method must take a M& to work correctly (like in laserlight's example).
    Apart from that, I am not sure if there is a template parameter for the object needed. Both Saving and Current derive from Account, so when inserting the object into the corresponding map, this should work, shouldn`t it? (I am not in the mood to test it myself, I must admit).

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,193

    Re: How to create a function for storing object in map?

    A common index to be used in different kinds of maps seems odd to me.
    Agreed. As I said in the post, this was just a simple example that compiles. Ind could be a static member of Create().

    Create method must take a M& to work correctly
    Agreed. Sorry. I've updated the code.

    IMO the OP has a class design issue and as I indicated in another thread, the classes should be polymorphic.
    Last edited by 2kaud; January 21st, 2016 at 05:28 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: How to create a function for storing object in map?

    Quote Originally Posted by Richard.J
    Apart from that, I am not sure if there is a template parameter for the object needed. Both Saving and Current derive from Account, so when inserting the object into the corresponding map, this should work, shouldn`t it? (I am not in the mood to test it myself, I must admit).
    It would, if the corresponding argument was a std::map<int, Account> object. However, in the example code from post #1, the corresponding arguments may be a std::map<int, Saving> object or a std::map<int, Current>. These are completely separate types from std::map<int, Account> (even after Account has been fixed to be a proper polymorphic base class), so defining a single Create function with a std::map<int, Account> reference parameter is not enough.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jul 2001
    Posts
    27

    Re: How to create a function for storing object in map?

    Thanks friends,

    Yes you are right i also found that my this code design have many problems so i changed my mind and drop this idea. Actually i am creating this program to show power of OOPs but i think this in not a proper way. I created a new design and new type class now i have problem with storing map in file but i will post that as separate question.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: How to create a function for storing object in map?

    Oh wait, I just realised that 2kaud's example from post #5 has two template parameters, so Richard.J's "template parameter for the object" comment refers to the second one. My bad. Yeah, it certainly is not needed.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,193

    Re: How to create a function for storing object in map?

    Storing/retrieving container data to/from a file is called data serialization. Have a look at
    http://stackoverflow.com/questions/8...dmap-to-a-file
    http://stackoverflow.com/questions/1...t-from-to-file
    http://stackoverflow.com/questions/2...e-a-class-in-c
    http://www.codeproject.com/Tips/4951...n-in-Cplusplus

    there is a lot of info about this on the internet.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.7.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)