Concretely, each object (of type VectDse
, MatrDse
, MatrStr
, MatrSps
, UPolDse
or MPol
, used as template-arguments for our containers) is attached with a counter whose purpose is to keep track of the number of references to its associated object. An assignment to a new object will increment the counter and copy the actual data's address. A modification of the shared object will induce a copy of the data leading to a new shared object with counter 0. The object's destructor either decrements the counter (if it is positive) or else effectively destroys the data, thereby freeing its storage.
The overall scheme is implemented by the class shared_object<R>
. Therefore, returning such objects by value in a function as in
VectDse<C> F(...) { VectDse<C> W= ...; return W; }
does not induce a performance penalty. Indeed a copy and destruction of W
will in this case just increment or decrement a counter.