not sure if this is the proper thread to post this question to, if not I apologize and will gladly post it where it needs to be. I am having an issue with understanding how to use multiple data types in one map container. For example my code looks like this:

struct A

{

string mName;

int mNumber;

};

struct B

{

string mName;

float mFloater;

bool mFlag;

};

class C

{

C();

~C();

//basically I want this map to hold a list by reference int for the entire collection of both objects from A and B.

map<int, A/B> myMultiMap;

}

so that in main..

int main()

{

C* myC;

A* myA;

B* myB;

myC->myMultiMap[0] = myA;

myC->myMultiMap[1] = myB;

}

is there any easy way to do this. I haven't had much luck with getting the boost libraries to load correctly, I've only been at this about a year, and don't completely know how to do some of the things. I appreciate any help that is offered, even ifs its just a link or something. Thanks for reading.

A better design would be to use std::shared_ptr (or boost::shared_ptr if your compiler doesn't support the C++11 standard) instead of raw pointers.

As for this point...wouldn't you say this advice is a bit misleading? Yes, shared pointers have their place, but shouldn't the train of thought be "by default, try to make sure a pointer is owned by only one object, and if that is impossible or breaks the design of the application, then use a shared pointer"?

My point wasn't to make a blanket rule for all pointer usage (so I'm not sure why you read that into my post) but rather to point the OP in the direction of smart pointers as a preference over raw pointers.

Thanks for advice guys, Ill try and make it more clear what I am trying to accomplish.

In visualization terms think of a box, this box can hold exactly 9 objects in it,each of the 9 spots is a numbered index 0-8, but only objects of either type A or type B. I would like a associative container that can keep track of which everything in the box, including which type is located at which box index. I tried doing this through inheritance and downcasting but I feel like there should be a better way. I have data members in A and B that extend a baseclass. The classes look similar to this:

//main.cpp
int main()
{
A* myObjA = new A("Steve", 0, "is a man", 4, 6, 7, 17, 8, 3.4f);
Box* myBox;
myBox->AddObjectA(myObjA);
/* during the above call I would like to set myObjA into the Box
at the first available index without another object. It tests
this in the function by checking if name = "". */
}

I hope that better explains it. As it currently stand I'm getting access violation errors when in the function to add. Those errors are keeping me from knowing if this set up will actually work for what I want. Thank you again, for reading into this. I truly appreciate the quick responses you have offered.

Why pass a shared_ptr? Because in your code you never check if the item was successfully added. You ownership is completely unknown at this point. If you delete it at the calling site and it was added -> access violation. If you don't delete it and it wasn't added -> memory leak.

Why a shared_ptr? Without knowing the usage of that box, it is safer to allow code that is currently using an item from the box to keep it alive, just in case it gets removed from the box in between. Using a smart pointer also means there is no need to explicitly delete an object in the box before replacing it (if that is even a use case).

Your main function (as explained above) also has a big issue.

int main()
{
Box myBox; //This has no business being a pointer in the first place
myBox.addItem(make_shared<ItemA>("Steve", 0, "is a man", 4, 6, 7, 17, 8, 3.4f);
}

The real problem is of course trying to stuff two completely unrelated types into the same container. Why? It will just result in a huge mess of if/else and dynamic_casts or getType(). Put them in two different arrays and let the box class hide that fact. Of course if you ever want to access an item in the box, you're back to the same problem. What type should that function return?

Generally you want to hide all members and use a _common_ interface for A and B. Depending on what they are supposed to do, try to abstract that. You want them to print their data? Have a virtual function "print" in both of them (and the base class). Don't even think about placing that code outside the classes and have it rip the data from their guts.

Hi Trienco, Thanks for the help and the advice, what you present seems to be a good solution to go off of. I think my originial intention with the map container over the array was that I wanted to have a string key that I could easily reference the items by. The items in the container are similar and inherit from an abstract base item class. The end game was to use this as part of an inventory system for a game. Not necessarily what the player is carrying but what they are storing into an external container in game. This container has actions that it takes much like an AI player. I was really tied to the idea of item lookup through string key. Thank you all for your comments and help. I'll be thinking through this much more in depth.