mercredi 25 février 2015

Replace class new[] variables with vectors - move, copy operators

I made a sparse matrix class for some work I am doing. For the sparse structures, I used pointers, e.g. int* rowInd = new int[numNonZero]. For the class I wrote copy and move assignment operators and all works fine.


Reading about the move and copy semantics online, I have tangentially found an overwhelming opinion that in modern C++ I should probably not be using raw pointers. If this is the case, then I would like to modify my code to use vectors for good coding practice.



  1. I mostly have read vectors over raw pointers. Is there any reason not to change to vectors?

  2. If I change the data to be stored in vectors instead of new[] arrays, do I still need to manually write copy/move assignment and constructor operators for classes? Are there any important differences between vector and new[] move/copy operators?

  3. Suppose I have a class called Levels, which contains several sparse matrix variables. I would like a function to create a vector of Levels, and return it:




vector<Levels> GetGridLevels(n, ... ) {
vector<Levels> grids(n);
... Define matrix variables for each Level object in grids ...
return grids;
}


Will move semantics prevent this from being an expensive copy? I would think so, but it's a vector of objects containing objects containing member vector variables, which seems like a lot...


Aucun commentaire:

Enregistrer un commentaire