I cant figure out the best way how my database should look like, so my problem is the following:
I want to create a windows gui program, where i can get informations about Products. First i want to write my classes i'm going to use in this windows form, following the following logic:
In my code everything is a Product thus there is a Product base class, for example a car is a product, but also it's wheel is a product in a factory that produces wheels, and both of them have a productID.
A car contains a wheel, so i want to pick a specific type of wheel and put it in the car. The car refers to the wheel as a Product, because this way i can store everything in the same array or vector, the wheel, the tire, the seat, etc. My program is going to get the products from a data type class, and here comes my problem:
How to store these kind of datas? I had 3 ideas, maybe they all are wrong (in this case please suggest a better solution)
1. Store all the Product pointers in a std::vector<Product* > vProduct;
cons:
I couldn't figure out a resource saving solution to get datas from this vector. For example, if i want to check all the wheels, i have to
-Go through the entire vector (may contain a lot of elements)
-1.1 Dynamic cast everything to get what type of object it is, or
-1.2 Write a virtual identifier getter method, where i can check if the element is a wheel, tire, etc.., but this doesnt sound quite elegant, because it's like if you put a label with the text "car" on a car.
2. Save the datas into separate vectors or arrays, like: std::vector<Wheel* >...
pros:
-Easy to use in drop down lists
-Uses less resources if you know what you are searching for.
cons:
-Makes the code a bit more complicated.
If there are only a few different product types and addition of new product types in the future is is exprected to be unlikely/rare, option 2. may be a good idea.
Otherwise, a multimap where the key is the type (std::type_index) of the product and the mapped value is the (smart) pointer to the product would allow fast, convenient look up for all products of a specified type.
This is an amazing solution from which I've learnt a lot, thank you. I was just wondering if the first and second iterators returned by std::unordered_multimap::equal_range() could have been used instead, thereby removing the need for struct product_range and template function all_of_type?