I am trying to write a linked list that will store a type double(prices), a type string(product name), and an int(barcode). The basis for this is that I am making a concession stand simulation. I already have 2 classes one for a person (dynamic array) to allow a group of any size. Another class for them to go through a line (queue) and purchase tickets. My dilemma is that I want to be able to allow each person to buy any number of snacks (add, remove, total for each person and a group total). In terms of design I'm not sure which class should it be in, and the best way to implement. I would love just an explanation so I can try and code it myself(make my own mistakes) and figure it out. Thank you as always!
I made both of my classes template classes. My person class has template<class S> and my queue class template<class L>. So if I'm understanding you correctly and from the link I would have my struct take either type S or type L. That would allow me to add any data type in as a reference at that point? To complete the thought my struct would need (to be complete) node type for head, first and last. Then all my data variables either S or L?
I am still a little confused on how I would be able to use my pointer for each person(random access) and use the chaining of the linked list so I can also access the orders of which ever person I want? I hope I am explaining myself well enough sometimes I feel I am not
this same concept can be cleaned up with a union or, with a significant effort, templates can be used to hide all the hands-on type switcheroos. And *every type* can be stored in a byte * (unsigned char or uint8_t etc) or vector thereof. Its all doing similar things under the hood (templates do it differently by crafting different actual object types instead of mushing bytes into a blob approach) -- a vector of bytes or templates are the way to go. Templates and polymorphism is really the only professional answer if you need to support more than a small # of types, but if you have not gotten to these ideas yet, its going to be challenging.
vector<unsigned char> blob(4*sizeof(int));
int *ip = (int*)&(blob[0]);
ip[0] = 5;
ip[1] = 13;
etc.
and a list of objects of vector<unsigned char> + type of the thing
JLBorges yes that is what I was hoping to see if I can do!! Jonnin I have yet to work with enum types, so to just get it working I will try JL's approach. When I learn of enum types or even a better (optimized), cleaner way I am going to try that as well. Thank you all, I always so this but, becoming a programmer has been eye opening in the sense that everyone is truly here for one another.
Happy Coding!
enum {x,y,z}; is the same as
const int x = 0;
const int y = 1;
const int z = 2;
Again, what I was showing you isn't better. It was the 'everything is really a pile of bytes and you can beat it to death' answer. And the reason is that I think for this question, the template / polymorphism answer is going to be fairly involved esp for a new coder.