I work on graphical applications and use generic and unique pointers, essentially because it handles freeing memory for me (as well as convenience), which is probably bad (if that's the reason I use them).
I recently read an answer to a question about Stackoverflow, which mentioned that, according to B. Straustup, unique / general ptrs should not be used at all, and that arguments should be passed by value.
I have a case in the chart for which I think using shared_ptr makes sense, but I would like to know from experts (I am not an expert in C ++), if I took up / thought about it, and if so, then what will they do instead (to be consistent with C ++ recommendations and efficiency)
I will take a general problem arising in Rendering / Ray-Tracing. In this specific task, we have a pool of objects (we will use triangles for this explanation) and a structure, which for simplicity of explanation we will call a regular three-dimensional grid. Let's say that at some point we need to insert triangles into the grid: what does this mean that we need to check the bounding volume of each inserted triangle, overlapping any of the cells from the grid, and this happens, then each overlapping cell needs to save a pointer / link to this triangle (for later use). A triangle can overlap more than 1 cell, so multiple cells can be referenced by several cells (you see where I am going with shared_ptr here).
Note that outside the grid structure we do not need a pool of triangles (so technically the object that owns the triangle pool here is a grid or, more precisely, grid cells).
class Grid { struct Cell { std::vector<std::shared_ptr<const Triangle>> triList; }; void insert(triangle*& tri_) { std::shared_ptr<const Triangle> tri = tri_; for (each cell overlapped by tri) {
It sounds complicated, but my motivation for this design is to follow the Stroustrup recommendation. In this case, the createPoolOfTrianglesAndInsertIntoGrid function createPoolOfTrianglesAndInsertIntoGrid not own triangles, this is done by the mesh cells. Therefore, I allocate memory in the createPoolOfTrianglesAndInsertIntoGrid function, because I need to create triangles here, and then I use the new placement method to get a pointer to each triangle in this pool, which I can then transfer to the insert grid (I cancel the memory management of this object by this method). There, it converts the triangle to shared_ptr , and cells can now share a link to it (using shared_ptr ).
I wanted to know, in your opinion, this is happening in the right direction, or if it seems completely wrong, both from the point of view of implementation and from the point of view of a possible loss of efficiency (I allocate a memory pool and then use a new place to create a temporary triangle which I then pass to the grid insertion method, then convert to shared_ptr, ...)
I try to both learn and improve my code and hope that you can provide valuable professional feedback.
EDIT: I am basically trying to find the right approach to this problem. + I will try later to make some changes based on your comments.