2

In my code I have a static class to store objects. To do stuff with inherence (casting etc) I store the objects in a map with unique_ptrs.

std::map<std::string, std::unique_ptr<DummyClass>>Map;

The stored objects have some member variables I want to update in my code (via variable bindings) so I pass out a reference to the stored objects to being able to modify them.

DummyClass& GetObject(std::string name)
{
  return *Map[name];
}

To update the members I store them in another map, in another class, to bind them to a string (I get the values via TCP/IP), something like this:

updater->registerVariable("bindingString", dummyObject.position.x);

The registerVariable Method also takes a reference (to dummyObject.position.x) and stores it in it's map.

std::map<std::string, std::vector<float&>> floatBindings;

But here raises the problem: I can't store references in the bindings map.

Anyone has a good idea what I could do instead? Would shared_ptrs the way to go? But the thing is that I don't really want do use shared_ptr s because the owner should only be the static class.

Priya
  • 334
  • 3
  • 8
str0yd
  • 97
  • 11
  • 6
    Good old fashioned, non-owning, raw pointers? Or [`std::reference_wrapper`](https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper)? – BoBTFish Aug 29 '18 at 13:23
  • You can also store `std::shared_ptr` and return `std::weak_ptr`. This way Map mostly retains ownership and you can check if the pointer has been deleted before using it in `floatBindings`. – Sorin Aug 29 '18 at 13:38
  • First figure your chain of ownership out: who owns those objects? Who's allowed to either share ownership (`std::shared_ptr`) or just access the object if it hasn't been deleted yet (`std::weak_ptr`)? – Marco A. Aug 29 '18 at 13:57
  • Will the element in your *map* always live longer than the parts that use the reference to it? – Galik Aug 29 '18 at 14:05
  • @Galik yes, the elements in the map will always be deleted last. Thank you guys for the answers so far! – str0yd Aug 29 '18 at 14:13
  • 1
    Note that your `GetObject()` will fail miserably if the specified `name` does not exist in the `map`. `Map[name]` will return a default-constructed `unique_ptr` holding a `nullptr`, and then `operator*` will have *undefined behavior*. If `name` doesn't exist, you should either raise an exception, or else create a default object to return a reference to. – Remy Lebeau Aug 29 '18 at 14:38
  • @RemyLebeau one can always check the string exists as a key in the map right ?! you can use reference_wrapper I have had good experiences with it ! – AdityaG Aug 29 '18 at 14:40
  • @AdityaG yes, the `name` can be checked for existance, by using `std::map::find()`, but the function would still need to decide what it should do/return in that situation – Remy Lebeau Aug 29 '18 at 15:29
  • Thanks for advide, I do check the map content, don't worry. Just tried to ceep it small. – str0yd Aug 29 '18 at 19:50

1 Answers1

0

Answered here: https://stackoverflow.com/a/922455/8894272

The component type of containers like vectors must be assignable. References are not assignable (you can only initialize them once when they are declared, and you cannot make them reference something else later). Other non-assignable types are also not allowed as components of containers, e.g. vector is not allowed.

This is basically the same since I tried to store a vector of references (in a map).

str0yd
  • 97
  • 11