我试图了解何时是使用随附的一些结构的合适时间,并且对使用with referenceboost有疑问。boost::optional
假设我有以下课程,使用boost::optional:
class MyClass {
public:
MyClass() {}
initialise(Helper& helper) {
this->helper = helper;
}
boost::optional<Helper&> getHelper() {
return helper;
}
private:
boost::optional<Helper&> helper;
}
为什么我要使用上面的而不是:
class MyClass {
public:
MyClass() : helper(nullptr) {}
initialise(Helper& helper) {
this->helper = &helper;
}
Helper* getHelper() {
return helper;
}
private:
Helper* helper;
}
它们都传达了相同的意图,即getHelper可以返回null,调用者仍然需要测试是否返回了助手。
boost::optional如果您需要知道“价值”和“非价值”之间的区别,您是否应该只使用nullptr?