I am making this simple game in SFML and I have this vector of pointers std::vector<Brick*> bricks; which stores objects of class Brick.Classes Brick and Ball derive publicly from class Object, because I have a polymorphic function to check colissions between 2 objects (Ball object is sf::CircleShape and Brick object is sf::RectangleShape).unsigned Engine::collisionCheck(Game::Object *object1, Game::Object *object2){    /* 1 - top collision, 2 - right, 3 - down, 4 - left */    if (object1->getDown() == object2->getTop() && inBounds(object1, object2) == 1)        return 1;    if (object1->getTop() == object2->getDown() && inBounds(object1, object2) == 1)        return 3;    if (object1->getLeft() == object2->getRight() && inBounds(object1, object2) == 2)        return 2;    if (object1->getRight() == object2->getLeft() && inBounds(object1, object2) == 2)        return 4;    return 0;}I want to destroy a brick when it gets hit by a ball so I erase it from the vector.for (unsigned i = 0; i < bricks.size(); i++) {        if (collisionCheck(ball, bricks[i]) == 1 || collisionCheck(ball, bricks[i]) == 3) {                                 double direction = ball->getCenter() - bricks[i]->getCenter();            direction /= bricks[i]->getWidth() / 2;            if (direction != 1.0)                ballMovement.x = direction;            ballMovement.y *= -1;               bricks.erase(bricks.begin() + i);                               }        if (collisionCheck(ball, bricks[i]) == 2 || collisionCheck(ball, bricks[i]) == 4) {            ballMovement.x *= -1;            bricks.erase(bricks.begin() + i);                   }    }My question is: should I destroy the object before erasing it, because when I do it my game crashes?Also, do I need to store an array of pointers instead of objects? Because the polymorphic functions takes pointers as arguments, so I see no other way.
C++ Erasing an object from vector of pointers
c++;vectors;collision;polymorphism;sfml