0

Is there anything in C++11 STL that is equivalent or similar to objc_setAssociatedObject/objc_getAssociatedObject in Objective-C ?

If not then is there any way by which i can achive the same behaviour in C++ ?

Community
  • 1
  • 1
Partho Biswas
  • 2,290
  • 1
  • 24
  • 39
  • Why not use `std::map` or `std::unordered_map`? – Justin May 22 '17 at 07:31
  • There is no equivalent in C++. Objective C objects seem to effectively be a `map`. C++ objects don't even have to exist at runtime. `objc_setAssociatedObject` and `objc_getAssociatedObject` directly access that underlying map (if I understand correctly) – Justin May 22 '17 at 07:32

1 Answers1

0

No. There is nothing equivalent to obj_setAssociatedObject.

You can get close with a static std::unordered_map, but the really difficult bit is ensuring that deleting the owning object deletes the associated object. If you don't need that, or you can modify the owning object code to have it's own map, then you should be fine.

If you do need to delete an associated object when the owning object is deleted, and you can't alter the code of the associated object, then we need more details of your application to make sensible suggestions (I suggest asking another question rather than changing this one).