I have a couple classes in my code, of which I need to copy objects.
However, some of those classes use data which I would like to skip when copying, like owner pointers.
So far, the only way I've found of doing this either to avoid copying entirely and construct a completely new object by hand every time I need to copy one, or wrapping the owner pointer in a private, non-copyable struct, like this:
class MyClass {
// non-copyable owner
struct Owner {
Owner() = default;
~Owner() = default;
Owner(const Owner& o) = delete;
Owner& operator=(const Owner& o) = delete;
SomeOtherClass* pointer = nullptr;
};
Owner owner;
}
However, doing it this way seems verbose and also invalidates all default marked copy constructors and operator='s.
(Note I would not like to use std::unique_ptr as I do not wish to deconstruct the owner when the object is deconstructed)
Is there a more compact / efficient / readable way of doing this? Is there a way to do this without invalidating default marked copy operations?
Aucun commentaire:
Enregistrer un commentaire