class MyClass { private: std::list<std::string> name; public: MyClass(void); void add(std::string name); //добавляет в список ~MyClass(void); }; 

How to make a copy constructor? Do I need a copy constructor when using list, vector, array, etc. (it seems to me that I need it)

  • Specifically in this case is not needed. - nitrocaster
  • and if the class will inherit or create multiple instances of this class (MyClass mc1, mc2)? And when will it be needed? - miyu
  • @miyu: Obviously, the copy constructor is needed when the default copy constructor does not fit. For example, if you have pointers in class members. - VladD
  • I would in most cases that are containers prohibited copying at all. When transmitting to somewhere would use links. - nanotexnik

1 answer 1

The copy constructor will be created by the compiler. This default copy constructor for each member calls the copy constructors. In this case, this is exactly the behavior that is required.

In the light of C ++ 11, it is better to indicate this explicitly:

 MyClass(const MyClass&) = default;