首页 > TAG信息列表 > stdoptional

c – 是否可以将对象设置为null?

在我的代码中,我检查以查看对象是否为空/空. 有没有办法将对象设置为null?解决方法:类的对象不能设置为NULL;但是,您可以将指针(包含对象的内存地址)设置为NULL. 你不能做什么的例子: Cat c; c = NULL;//Compiling error 你可以做什么的例子: Cat c; //Set p to hold the memory ad

c – 使用std :: optional避免函数中的默认参数是否有任何优势?

我正在将代码移植到C 17,尝试尽可能使用新功能.我喜欢的一个想法是使用std::optional返回或不返回某些条件下可能失败的函数中的值. 我很好奇这个新功能的可能用法,我正在考虑开始用它来替换函数中的可选参数,所以: void compute_something(int a, int b, const Object& c = Object

c – 为什么std :: optional :: value()\u0026>返回\u0026\u0026?

我有运行时错误,当使用std :: optional替换一些代码时: 旧代码: T getValue(); ... const auto& value = getValue(); value.get(); 新代码: std::optional<T> getValue(); ... const auto& value = getValue().value(); value.get(); // Runtime error, crash 这对我来说无法预