C++ DAL - Return Reference or Populate Passed In Reference

后端 未结 2 514
天涯浪人
天涯浪人 2021-01-06 00:46

[EDIT 1 - added third pointer syntax (Thanks Alex)]

Which method would you prefer for a DAL and why out of:

Car& DAL::loadCar(int id) {}
bool DAL         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-06 01:02

    Since you are anyways allocating objects on heap, why not to consider Car * LoadCar() which returns NULL if problem occurs. This way you have no restrictions with reference types (each reference must be initialized) and also have means to signal the error case.

提交回复
热议问题