I'm trying to "modernize" some existing code.
- I have a class which currently has a member variable "Device* device_".
- It uses new to create an instance in some initialization code and has a "delete device_" in the destructory.
- Member functions of this class call many other functions that take a Device* as a parameter.
This works well, but to "modernize" my code I thought I ought to change the variable to be defined as "std::unique_ptr<Device> device_"
and remove the explicit call to delete, which makes the code safer and generally better.
My question is this -
- How should I then pass the device_ variable to all of the functions that need it as a paramater?
I can call .get to get the raw pointer in each function call. But that seems ugly and wastes some of the reasons to use a unique_ptr in the first place.
Or I can change every function so that instead of taking a parameter of type "Device*" it now takes a paramater of type "std::unique_ptr& ". Which (to me) somewhat obfuscates the function prototypes, and makes them hard to read.
What is best practice for this? Have I missed any other options?
See Question&Answers more detail:os