Good Code
The good version stores standard-library values, so generated copy, move, and destruction rules are enough.
Lesson 05
Prefer standard-library members so copy, move, and destruction follow generated behavior instead of manual pointer code.
#include <string>
#include <utility>
#include <vector>
class ReviewSummary {
public:
ReviewSummary(std::string title, std::vector<int> scores)
: title_(std::move(title)), scores_(std::move(scores)) {}
// Standard members keep copy and move behavior generated by the type.
private:
std::string title_;
std::vector<int> scores_;
};#include <cstring>
class ReviewSummary {
public:
explicit ReviewSummary(const char* title)
: title_(new char[std::strlen(title) + 1])
{
std::strcpy(title_, title);
}
// Manual destructor without copy control makes copies share one pointer.
~ReviewSummary()
{
delete[] title_;
}
private:
char* title_;
};The good version stores standard-library values, so generated copy, move, and destruction rules are enough.
The bad version owns a raw character buffer and defines only a destructor. The compiler still generates copying, which can make two objects delete the same pointer.