Optional function parameters: Use default arguments (NULL) or overload the function?

前端 未结 12 1543
情书的邮戳
情书的邮戳 2020-12-08 00:44

I have a function that processes a given vector, but may also create such a vector itself if it is not given.

I see two design choices for such a case, where a func

12条回答
  •  死守一世寂寞
    2020-12-08 01:01

    The first way is poorer because you cannot tell if you accidentally passed in NULL or if it was done on purpose... if it was an accident then you have likely caused a bug.

    With the second one you can test (assert, whatever) for NULL and handle it appropriately.

提交回复
热议问题