How to remove a Pointer to an Object in a list of Pointers in the class Destructor?(C++)

Posted by Zestyclose-Window358@reddit | learnprogramming | View on Reddit | 13 comments

inline std::vector<GameObject*> GameObjects;

i have a dynamic list that Contains pointers to objects of the class GameObject
in My constructor i add the pointer to point to the object itself:

GameObject::GameObject(glm::vec2 Pos,const std::string &TexturePath) {
    this->pos = Pos;
    GameObjects.push_back(this); <- this line


   std::vector<float> Vertices = build_cube_vertices();
    vertex_setup(Vertices,Indices);
    texture_setup(TexturePath);
}

In my Destructor how do i remove the Pointer to the Object to avoid dangling Pointers?

Thanks for reading