Computers |
|
| | #1 (permalink) |
| Super Techie Join Date: Jun 2004
Posts: 348
| Hey people, I am writing a uni program and have encountered a bit of a problem. Ill set you up with a basis of what i am trying to do. I am writing a simulator for a toll booth system. In this i am using a queue abstract to simulate the queues at the toll booth. The number of queues needs to be specified at run time. I am wondering if there is a way i can attatch a variable to the end of a name to make each queue unique. Basically i want to do something like this: for (int i = 1; i <= numQueues; i++) { QUEUE que&i; que&i = createQueue(); } The bit i am asking about is the &i part. i know this isnt the correct way to code this, if there is a correct way. But if anyone can give me an idea of what to do, it would be VERY helpful. |
| | |
| | #2 (permalink) |
| Ultra Techie | Use an array of queues. That way you can just refer to them by their index. I haven't used C++ in a while, in C# you'd use an ArrayList (an array that can have a variable number of elements), so I'm not sure if C++ has something similar. Just add a new "toll booth" to the array for every toll booth you need.
__________________ ![]() See today\'s Penny-Arcade!(May contain foul lanuage) Pain is weakness leaving the body. PM Me for my MSN |
| | |
| | #4 (permalink) |
| Ultra Techie | It's pretty simple. In C# (I know you're using C++, but i'm not too keen on that) You'd make the arraylist then just do something like: Code: ArrayList arrList = new ArrayList(10);
for(int i = 0; i < 10; i++){
arrList.Add(new Queue());
}
// then you'd access it like this
arrList[1].SomeMethod(); // do stuff with the second queue (since it's zero based)
arrList[4].SomeOtherMethod(); // do more stuff with the 5th queue
//etc
__________________ ![]() See today\'s Penny-Arcade!(May contain foul lanuage) Pain is weakness leaving the body. PM Me for my MSN |
| | |