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

前端 未结 12 1542
情书的邮戳
情书的邮戳 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:09

    I usually avoid the first case. Note that those two functions are different in what they do. One of them fills a vector with some data. The other doesn't (just accept the data from the caller). I tend to name differently functions that actually do different things. In fact, even as you write them, they are two functions:

    • foo_default (or just foo)
    • foo_with_values

    At least I find this distinction cleaner in the long therm, and for the occasional library/functions user.

提交回复
热议问题