+ 1
Why copy constructor in c++?
4 ответов
0
It's also important to understand:
1) There is always a copy constructor, whether user-defined or from the compiler.
2) The compiler will create a simply bitcopy (shallow copy) - this can be problematic if the object contains it's own pointers or non-shared references to files. That's where user-defined copy constructors save the day, you control how the class is recreated (copies do not have to be identical!)
3) If you pass a class instance by value in any way (to a function for example) - a temporary object is created by using the copy constructor.
4) Just as important are the destructors - don't forget them. Rule of thumb is that if a user-defined copy-con is required, then a decon is also required (along with overloading the assignment operator=)