C ++: Instances of the same class

1

I have in a project a class Collision , and to make a more efficient detection, I wanted to have access to all instances (objects) of that same class. Is there any easy way to do this?

Edit 1
I did so: I created a class called world , which stores all objects and collisions, where I have the isColliding method, which checks the collision between one object and all others, or between two objects.

    
asked by anonymous 02.06.2017 / 10:49

1 answer

1

In order not to be left without a response record, I answer here what has already been answered in the comments and made by the OP: create a container with a pointer for all objects created, inserted at the time of construction. I suggest doing using a binary tree ( std::set ), to reduce complexity:

class Collision;

std::set<Collision> world;

class Collision {
public:
    Collision() {
        world.insert(this);
    }

    ~Collision() {
        world.erase(world.find(this));
    }
}

This way you can find all Collision instantiated in your program.

    
25.07.2017 / 22:29