I have such a class method

class IOManager { public: std::vector<std::string> getWordsFromString(std::string line) { std::vector<std::string> StringVect; //code return StringVect; } }; 

And it is used in the child class like this:

 class ConsolIOManager : public IOManager { public: void SetHashTable(IOManager *obj, std::unordered_map<ull, std::string>& HashTable) override { for (auto &It : obj->getWordsFromString(obj->getText())) { //code } } } 

What is the best way to return a vector in this case ? Under the link or as in the current code copying?

  • If this is C ++ 17, then there is no copying when returning a vector. But it’s better to accept the line by reference - VTT
  • @VTT why? Does c ++ 17 have any changes in vector? - AlexIdest
  • one
    No, in C ++ the rules for initialization and optimization of return values ​​were radically reworked. Now, when returning a variable by value, as in this case, neither copying nor moving occurs. - VTT
  • On the other hand, if the caller can reuse the vector, then it makes sense to accept the sending so as not to overestimate the space in the vector each time. - VTT
  • @VTT No, the vector does not correspond, so, apparently, you should not take the link. I'm going to read about C ++ 17, thanks - AlexIdest

0