June 2025
Intermediate to advanced
1093 pages
33h 24m
English
You have learned two ways to copy a class instance. Let's assume your class is called Image:
Image(const Image& other)—the copy constructorThis can only happen during the initialization of a new object.
Image& operator=(const Image& other)—the assignment operatorThis overwrites and only happens with an already existing object.
In most cases, all data fields from the source are copied into the target. Of course, this can be very costly.
// https://godbolt.org/z/75Kh5Ws45#include <vector>class Image { std::vector<std::byte> data_;public: explicit Image(const char *fn) { /*...*/ } // Compiler generates (among others): // Copy constructor, assignment, but also moves};std::vector<Image> loadCollection(bool empty) {
Read now
Unlock full access