Ich brauche meine MyObject-Klasse, um einen Verweis auf die besitzende MySet-Instanz zu haben, etwa so:
Code: Select all
class MySet;
class MyObject
{
public:
...
private:
MySet &m_owner;
...
};
Derzeit wechsle ich dazu, einen Zeiger anstelle einer Referenz zu verwenden, aber das ist nicht ideal.
Ist es möglich, eine solche Referenz zu kopieren? Wenn ja, wie?
-------------------------------------
Zur Referenz: Der Kompilierungsfehler, den ich ohne Kopierkonstruktor erhalte, lautet:
Code: Select all
/x86_64-unknown-linux-gnu/include/c++/v1/__algorithm/move.h:42:17: error: object of type 'Object' cannot be assigned because its copy assignment operator is implicitly deleted
42 | *__result = _IterOps::__iter_move(__first);
| ^
/x86_64-unknown-linux-gnu/include/c++/v1/__algorithm/copy_move_common.h:109:19: note: in instantiation of function template specialization 'std::__move_loop::operator()' requested here
109 | auto __result = _Algorithm()(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__out_first));
| ^
/x86_64-unknown-linux-gnu/include/c++/v1/__algorithm/copy_move_common.h:133:15: note: in instantiation of function template specialization 'std::__unwrap_and_dispatch' requested here
133 | return std::__unwrap_and_dispatch(std::move(__first), std::move(__last), std::move(__out_first));
| ^
/x86_64-unknown-linux-gnu/include/c++/v1/__algorithm/move.h:112:15: note: in instantiation of function template specialization 'std::__dispatch_copy_or_move' requested here
112 | return std::__dispatch_copy_or_move(
| ^
/x86_64-unknown-linux-gnu/include/c++/v1/__algorithm/move.h:122:15: note: in instantiation of function template specialization 'std::__move' requested here
122 | return std::__move(std::move(__first), std::move(__last), std::move(__result)).second;
| ^
/x86_64-unknown-linux-gnu/include/c++/v1/vector:1532:32: note: in instantiation of function template specialization 'std::move' requested here
1532 | this->__destruct_at_end(std::move(__p + 1, this->__end_, __p));
| ^
my_set.cpp:92:24: note: in instantiation of member function 'std::vector::erase' requested here
92 | m_objects.erase(it);
| ^
object.h:45:9: note: copy assignment operator is implicitly deleted because 'Object' has a user-declared move constructor
45 | Object(Object &&other);
| ^
Code: Select all
/x86_64-unknown-linux-gnu/include/c++/v1/__algorithm/move.h:42:17: error: object of type 'Object' cannot be assigned because its copy assignment operator is implicitly deleted
Full version