We have lots of existing code using raw pointers, which is littered with null checks at almost every usage.
In trying to write newer code more cleanly, we're trying to use factory constructor methods and unique_ptrs.
My question is, in the code below, once we've got our factory-created object - sensorX - can we use that throughout the rest of the code without further null checks on it, since it's a const unique_ptr?
DeviceFactory.h
class DeviceFactory
{
public:
template<typename T>
static unique_ptr<T> create(int id, std::string status)
{
auto device = unique_ptr<T>{ new T{ id, status } };
if (!device) throw DeviceCreationException("device couldn't be created");
return device;
}
};
Usage
const auto sensorX = DeviceFactory::create<Sensor>(123, "sensorX");