-
January 20th, 2016, 12:31 PM
#1
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);
};
-
January 20th, 2016, 12:56 PM
#2
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.
-
January 20th, 2016, 01:08 PM
#3
Re: How to create a function for storing object in map?
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
}
-
January 20th, 2016, 11:59 PM
#4
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.
-
January 21st, 2016, 03:06 AM
#5
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 21st, 2016, 05:01 AM
#6
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).
-
January 21st, 2016, 05:22 AM
#7
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 21st, 2016, 09:39 AM
#8
Re: How to create a function for storing object in map?
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.
-
January 21st, 2016, 11:42 AM
#9
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.
-
January 21st, 2016, 11:54 AM
#10
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.
-
January 21st, 2016, 11:58 AM
#11
Re: How to create a function for storing object in map?
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++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|