vendredi 24 avril 2020

copy and move assignment operator

i wrote this code :

  #include <iostream>

  class Animal{

  protected:
      std::string name;
      std::string color;

  public:
      //constructor
      Animal(const char* n,const char* c) : name(n) , color(c) {}
      //destructor
      virtual ~Animal(){}
      //copy constructor
      Animal(const Animal &anm){
          name=anm.name; //or name(anm.name)
          color=anm.color;
      }
      //move constructor
      Animal(Animal&& _anm){
          name=_anm.name;
          color=std::move(_anm.color);
      }
      //copy assignment operator
      //move assignment operator



      //pure virtual function,we must to implement this method in each derivated class.
      virtual void makeSound()=0;
  };

  int main() {
      std::cout << "Hello, World!" << std::endl;
      return 0;
  }

and i have some problems with copy and move assigment operator.I really dont understand them and for my code,where i have 2 atributes name and color,both strings,how i should write these ?? And the second question : when to use overloading operator,it is similiar with this assignment operator ?

Aucun commentaire:

Enregistrer un commentaire