I have a function that is called every 16ms. In it I would like it to simulate aircraft entering and exiting an area of interest. Every aircraft is represented by a class which may enter or leave the area based on certain probability concepts.
To simulate this, I could create [xx] objects of the aircraft class at the start and then set a bool which tells me whether they are active, but this seems messy. [xx] is the max number of aircraft I would expect to be simulated at any given time. Is there a method to create and delete the objects as required instead of putting aside a given amount of memory from the start and using bits here and there? I'd love some keywords to research.
// operator new example
#include <iostream>
#include <new>
usingnamespace std;
struct myclass {myclass() {cout <<"myclass constructed\n";}};
int main () {
int * p1 = newint;
// same as:
// int * p1 = (int*) operator new (sizeof(int));
int * p2 = new (nothrow) int;
// same as:
// int * p2 = (int*) operator new (sizeof(int),nothrow);
myclass * p3 = (myclass*) operatornew (sizeof(myclass));
// (!) not the same as:
// myclass * p3 = new myclass;
// (constructor not called by function call, even for non-POD types)
new (p3) myclass; // calls constructor
// same as:
// operator new (sizeof(myclass),p3)
return 0;
}
But this only shows new classes being created during a single iteration. How would one keep track of multiple classes throughout several iterations of a function. I don't want all of these classes to be deleted every iteration, I would like to keep track of them until they leave the area 6000 iterations later.
Example: One iteration I may want to create an object:
If I name my class objects like this, you can see that I'd be limited by how many objects I would be able to handle at any given time and it would get messy. Is there any way to avoid this?
class aircraft {...};
int function_tick()
{
static aircraft* ActiveAC[100] = {NULL}; //Array of possibly active objects
staticint nb_ActiveAC = 0; //Number of active objects
//Step 1: Create a new object
if (aircraft_enters())
{
aircraft* ActiveAC[nb_ActiveAC++] = newsizeof (aircraft);
}
//Step 2: Process existing objects
for (int i=0 ; i < nb_ActiveAC ; i++)
ActiveAC[i].step();
//Step 3: Delete objects as required
for (int i=0 ; i < nb_ActiveAC ; i++)
{
if (ActiveAC[i].DeleteMe())
{
delete ActiveAC[i]; //Deleting object
for (j = i ; j < nb_ActiveAC-1 ; j++)
ActiveAC[j] = ActiveAC[j+1]; //Shifting newer objects down
ActiveAC[--nb_ActiveAC] = NULL; //Decreasing number of active objects
}
}
return 0;
}
Edit *Actually I think I'll have to read to figure out how to replace the array with a vector, but ignore that for now