I've been typing in the following example code from a book, which is shown in the following, demonstrating move semantics. It works up to the point where the desctructor message is output to screen, then crashes.
I could not determine what the course for this crash is, there is no other message shown than the window saying: Program has stopped working, and the logs give no indication as to where the error occurs or why.
So, my question is, what could be the source of the error? (My guess is that it has to do with strcpy (using strcpy_s does not solve the error, the program keeps crashing after call to destructor)). (Ex.: strcpy_s(name, strlen(obj.name), obj.name);)
Chervil, thank you for helping me! Unfortunately the copy constructor was not the problem. It was the overloaded assignment operator function, which in the book looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
// Overloaded = operator
Person & operator = (const Person &right)
{
cout << "*** assignment operator ***\n";
if (this != &right)
{
if (name != nullptr)
delete [] name;
name = newchar[strlen(right.name) + 1];
strcpy(name, right.name);
}
return *this;
}
Because there are no brackets separating the if statement, and I not paying attention to the indentation, put brackets underneath the inner if-clause, enclosing all three lines marked above ...
Thus, the problem is solved, lesson learned, and improvement as per your suggestion implemented. :-)