Returning a “NULL reference” in C++?

前端 未结 8 578
旧巷少年郎
旧巷少年郎 2020-12-05 00:02

In dynamically typed languages like JavaScript or PHP, I often do functions such as:

function getSomething(name) {
    if (content_[name]) return content_[na         


        
8条回答
  •  眼角桃花
    2020-12-05 00:09

    I can think of a few ways to handle this:

    • As others suggested, use boost::optional
    • Make the object have a state that indicates it is not valid (Yuk!)
    • Use pointer instead of reference
    • Have a special instance of the class that is the null object
    • Throw an exception to indicate failure (not always applicable)

提交回复
热议问题