I'm trying to design a class to design a 3D mesh in C++. My idea is the following: 3 base classes: Point, Polygon, Polyhedron (the mesh has to handle very general shapes) The class Polygon contains a vector of shared pointers to Point (the vertexes), in the same way, Polyhedron contains 2 vectors of shared pointers to Point and Polygon. It would be very useful to have inside Polygon a vector of shared pointers to Polyhedron and inside Point 2 vector of shared pointer of Polyhedron. In this way, every Point would know the polygons and polyhedrons connected to it.
I have already implemented the classes (using enable_shared_from_this
) in this way and all works quite well, but now I have some doubts about this.
Is this design a good design or it may lead to some problems? Is a bad behavior to have 2 objects connected by shared pointers? How is the memory management in this case?
The shared pointers are very important in the connection Polygon->Point, Polyhedron->Point and Polyhedron->Polygon, but not so necessary in the other direction. I thought that I may use a weak pointer instead of shared ones, in this way Point would not be responsible for Polygon and Polyhedron life. I prefer not to use standard pointers.
I would like to know your opinion, thank you very much!