Return a “NULL” object if search result not found

前端 未结 8 2121
一个人的身影
一个人的身影 2020-11-28 02:23

I\'m pretty new to C++ so I tend to design with a lot of Java-isms while I\'m learning. Anyway, in Java, if I had class with a \'search\' method that would return an object

8条回答
  •  春和景丽
    2020-11-28 03:03

    The reason that you can't return NULL here is because you've declared your return type as Attr&. The trailing & makes the return value a "reference", which is basically a guaranteed-not-to-be-null pointer to an existing object. If you want to be able to return null, change Attr& to Attr*.

提交回复
热议问题