lundi 23 octobre 2017

How to get rid of (one) unnecessary copy when constructing a vector?

I have a class A which handles my resources (quite large). Now I need a vector of these std::vector<A> vec_of_A (3, A()). Problem is that vector first constructs temporary object A(), then copy constructs three times A() and then destructs the first temporary object ~A(). As my A() allocates quite a large chunk of memory I end up (temporarily) with memory requirements 33% larger than I actualy need.

How to construct vector of A while avoiding unnecessary constructors? (My class A satisfies all rules of five).

Aucun commentaire:

Enregistrer un commentaire