c++ - When is it safe to add object values to vectors on the heap? -
Suppose i have a structure called a straight, which combines two shorts with one method:
structure head (float x, y, z) // make peak with the top of initial values (float ix, float i, float iz); // gives value c = this + b top operator + (top b); }; Top: Vertex (float ix, float, float is) {it-> x = ix; This- & gt; Y = iy; This- & gt; Z = iz; } Vertical Top: Operator + (Top B) {Top C; C. X = it- & gt; X + B.x; C. Y = it- & gt; Y + bi; C.z = is-> Z + b.z; Return c; } In another calling function, I want to add two corners together and the result will be vector & lt; Vertex * & gt; When is the value of the returned value used to add a given vector? If not, how do I apply it?
For example,
vector & lt; Vertex * & gt; V; Top A (1, 2, 3); Top B (4, 5, 6); V.push_back (and (a + b));
This is not secure, because you automatically accumulate an indicator in the temporary variable , Which will be recovered at the end of the current function.
There is a serious risk involved in dynamically allocated objects that are allocated automatically. Sometimes the best strategy is to completely reject the automatic allocation (for example, by making a constructor private and using a factory method to create a new instance). You will then be responsible for destroying them at some point.
The second option (not necessarily what you want) is to do everything by value, instead of vectorx * vectx is a vector, and when they If you are archived, just copy your class, the way your class is written, all the regions are priorities, so it can be quite good and you do not run into problems of performance or deep-copy meaning Gay.
Comments
Post a Comment