-4

Updated again to reflect the question little bit different this time and hope I can get a solution to this:

I can think of a store where there would be 2 workers - 1 worker's job is to receive products and store in appropriate bins and second worker's job is to pick the product based on customer's request and sell that to customer.

The store is expected to store 'N' products ( variable ) and each of these bins needs to hold only similar products.

How to go about solving this problem?

  • If container can share dissimilar items - I could have gone with defining a single container in shared header file and each worker would have operate on this shared container. The issue here is container should only hold similar items and unable to think of a generic way to define a shared containers for variable 'N' products.

  • I also have thought about pre-defining the containers for each of the product types - but the issue with this approach is - when a new product is added - I would have to update the header to file to define a new container - which I am trying to avoid - if there is any way to define this container dynamically - and can be shared between 2 workers.

Thanks

ejuser
  • 181
  • 2
  • 5
  • 13
  • something like `typedef std::multimap Map;` ? – Elazar May 16 '13 at 01:45
  • Dont' define variable in header. – billz May 16 '13 at 01:46
  • 6
    Why do you need this? This information will help us give you an appropriate answer. – Konrad Rudolph May 16 '13 at 01:49
  • 1
    each **program** will have their own copy of items anyway. It is not trivial to make it shared across processes. – perreal May 16 '13 at 01:58
  • Basically - I would like to use this in multiple programs - for which the manipulation is logic is same but the data content would be different. They need to operate simultaneously for each of those categories. One of the options I can think of is to store the category type in the container itself to manipulate but the size of the container and logic to manipulate the data would be tricky. If the container name itself is dynamically generated somehow based on the category of the program - I can just use the generic body of the program to manipulate the data. – ejuser May 16 '13 at 02:25
  • And as multiple programs needs to share the data - it has to be part of the common header itself. few of the programs populate the data and few programs read the data. The content would depend on different types of data but the manipulation logic is same - so manipulation code I would like to see a single version if possible. – ejuser May 16 '13 at 02:28
  • Why not make a single program and read the variable data from a file? – anthony sottile May 16 '13 at 02:41
  • Anthony - When a file gets involved - I/O operations are likely to slow down. Wanted to use this for realtime application where the response time is critical. I can always make different programs for each of the categories but wanted to avoid multiple copies of the program. Only difference is container identifier and if there is a way to use different container based on the category some how - it would solve the problem for me. – ejuser May 16 '13 at 02:47
  • In that case... you could potentially make a header which exposes a singleton through a function and then at link time link in whichever version of the data you want. – anthony sottile May 16 '13 at 02:59
  • 3
    -1: It is really not clear what should be shared with what, where and when. Please, explain what you are trying to do, in high level terms, i.e. starting from _user_ perspective, preferably in a new question (to avoid cluttering it with this long discussion thread). – Jan Hudec May 16 '13 at 06:59
  • Thank you all for trying to help. I have updated the question to reflect what I am after - a dynamic map name depend on external factor. Thanks – ejuser May 16 '13 at 11:19
  • Dynamically naming variables (what *for* still remains unclear) remains the least of your problems. If you are really talking about two independently running *programs*, this is a matter of inter-process communication (IPC), and the *naming* of the variable becomes *completely* secondary as one programm cannot "see" variables in another program, *at all*. It seems you have picked a problem that exceeds your current level of experience with the language... by a fair margin. – DevSolar May 16 '13 at 12:39
  • All I am trying to use dedicate a "named" container based on the product I am operating on rather than building a portion of program depending on the product. As I don't have the finite number of products to work with - I can't "pre-define" the container names based on the product(as the code is same for all the products) and is the reason for prompting this question. – ejuser May 16 '13 at 12:57
  • Poor approach to whatever problem you are trying to solve. State the problem itself and solutions will be offered that use the language abstractions to solve it appropriately. I guarantee you have not come up with a novel new problem that no one has solved before. – Amardeep AC9MF May 16 '13 at 13:22
  • @DevSolar / Amardeep - I have updated the original question with a hope that I have defined the problem a bit better now and looking for a generic solution. - Anthony Sottile - Would you please elaborate a bit about the solution you have proposed? thanks – ejuser May 16 '13 at 16:09

1 Answers1

1

You seem to be getting an awful lot of different concepts confused, certainly more than could be sorted out in this form.

One way to solve this would be a base class Product from which all your various "product types" derive, solving your first bullet point neatly, as any standard container could hold various types with a common base class.

But your description is so nebulous that I cannot tell if this would even be a viable solution for you...

My impression is that you are trying to solve advanced producer / worker problems without basic experience with OOP and C++. Start at the beginning, and work patiently through the examples of types, classes, objects and inheritance. If you do that, not only might you develop a better idea of what you are actually trying to achieve. You will also develop the vocabulary to ask questions in a way that could be answered with likewise precision. (Like, whether we are looking at a single-threaded "homework" style program, a multithreaded application, or multiple processes communicating with each other -- I am still not sure about that one.)

As it is, all we can do is guessing, and drivel in the general direction of what might or might not be a solution for what we understand might be your problem...

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Thanks for your reply. I actually have a working solution for a single product already - map of unique_ptrs to a class object. I have an option to an additional variable - product-type into this class and this becomes generic. But I am trying to avoid this route of adding "product-type" into this class. Instead trying to explore if I can define a dynamic map - based on the product I am operating against. It appears like - I might end up adding "product-type" itself to the class which would solve the problem.. Would explore further. Thank you !! – ejuser May 16 '13 at 17:03
  • @ejuser: Did you think about template functions that would decide whether to add to / remove from a `std::map< T >` depending on the type of their parameter? – DevSolar May 17 '13 at 02:36
  • As you have pointed out I have only recently started with c++ and can build based on other examples. I am not currently using template based functions. I still haven't given up on this task yet. Seems like "factory" method of creating objects might help in my case... exploring further.. – ejuser May 17 '13 at 12:05