dimanche 8 septembre 2019

Returning a new object from a object returning function c++

I'm working on a program that intersects according to set theory two sets represented by 2 objects. Each objects can contain 0 or more elements. The function is given and cannot be changed only the implementation inside.

In my code, I check the invoking object and the second object (otherIntSet) are empty, if yes, they intersect at the empty set. If they contain any elements I check to see if the element in data[] is contained in the otherIntSet. I use "return IntSet();" but all i get is empty set.

IntSet IntSet::intersect(const IntSet& otherIntSet) const
{
  if ( (this->isEmpty() ) && (otherIntSet.isEmpty() ) )
   {

    return IntSet(); 
   }
  else 
  {
    for (int i = 0; i < used; ++i)
    {
        if (otherIntSet.contains(data[i]) )
        {
            IntSet().add(data[i]);
            cout << IntSet();
        }
     }

}

}

I'm not sure how to return the new object created properly so that the elements that are added to it are actually saved. Thank you

Aucun commentaire:

Enregistrer un commentaire