I have a class MyObject . All instances of it must belong to MyObjectSet , and it would not be possible to build it anywhere else. Inside MyObjectSet I use std::vector<MyObject> to store all instances.
The problem is that for std::vector<MyObject> to work, the std::vector<MyObject> move constructor must be publicly available (adding std::vector<MyObject> as a friend of MyObject not enough).
class MyObject { MyObject(int n); friend class MyObjectSet; public: MyObject(MyObject&&) = default;
But if I make it publicly available, I could create an instance of MyObject from another place.
void SomeUnrelatedFunc(MyObjectSet& set) { MyObject m(std::move(set.GetMyObject(0)));
Is there any way to solve this problem?
You can save pointers to MyObject instances inside MyObjectSet instead, but I would like to avoid this if possible.
source share