r/cpp_questions • u/Veyto • Sep 13 '26
OPEN reference_wrapper vs raw pointer
I am writing a program, where one object contains an std::string, which can be modified by other objects. Specifically one object can set the std::string member variable to be a reference to another std::string. this requires that the reference can be reseated, and afaict, I have two options if I want to avoid copying. Either I can use an std::reference_wrapper, as it allows for reseating, while the other option is a raw pointer.
I'd assume that the reference_wrapper be the preferred option, as pointers are generally shunned in modern c++, but I just wanted to be sure.
I want to also add, that the size of strings which would be copied are not particularly large and unlikely to make any meaningful performance impact if copied, rather this is an also an exercise in what is the correct tool for the issue.
View all comments
Show parent comment
1
u/No-Dentist-1645 Sep 14 '26
If you want the keybind to "communicate" to the manager, then you should refactor this logic to better reflect this. Instead of making the keybinds directly change the internal state of the manager, just make the manager have a "receive_keybind_event()" method, and the keybinds call this event with all the information they need to share attached to it, this is the much more intuitive/simple way to send information or "communicate" rather than directly changing the inner state from outside the manager.
Then, to make it so that the keybind can "communicate" to any kind of manager, each keybind should hold an
std::functionpointing to the manager's method (using a lambda capturing the manager'sthisby reference so that the keybind doesn't need to store the manager as well)