function overloading vs default argument in c++

假装没事ソ 提交于 2019-11-29 08:42:27

Yes, you can replace your overloaded functions with one function and a default argument:

string conct (string a, string b, const char* c = "string") {
  // do the processing;
  return concatenated_string;
}
  • When you overload functions the compiler generates code for each function, probably resulting in larger code size.
  • If an overload just acts as a thin wrapper as in your case then the optimizer may eliminate the extra work.
  • default arguments get set at the caller's location, rather than inside the function, so default arguments must be publicly visible, and changing them requires recompiling all callers. With an overload like yours the psuedo-default argument becomes a hidden detail.

default values can be used in function prototypes but if we want to default middle argument then we'll have to default all values to its right... On the other hand overloading a function can be done for all possible argument combinations also default value needs not to be placed on function call stack and thus less work for the compiler...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!