mercredi 3 mars 2021

Vector as class member

class A
{
   private:
      std::vector<int>myvec;
   public:
      const std::vector<int> & getVec() const {return myvec;}
};
void main()
{
   A a;
   bool flag = getFlagVal();
   std::vector<int> myVec;
   if(flag)
      myVec = a.getVec(); 
   func1(myVec);
}

In the line myVec= a.getVec(), there is a copy of vector although it is returned by reference. If flag is not true, an empty vector will be passed. Anyway to avoid this copy ?

Aucun commentaire:

Enregistrer un commentaire