c ++重载函数默认参数 [英] c++ overloading a function default argument
问题描述
我有一个广泛使用的c ++库,它包含一个方法,例如:
foo();
/ p>
我想重载这个函数有一个默认参数,如:
foo (bool verbose = false);
这个更改是否强制我重新编译使用此函数的每个代码?
不能调用 foo()
没有参数保持工作作为no-args-signature没有更改?
顺便说一下 - 我使用gcc
感谢
这个更改是否强制我重新编译使用此函数的每个代码?
是的,编译将失败,因为会有歧义。
你可以做的是重载函数:
foo(bool verbose);
并将 foo()
如果参数为false。
这不需要重新编译。你只需要两个函数:
foo(){foo(false); } //可能
foo(bool verbose);
而不是具有默认参数的一个。
I have a widely used c++ library which holds a method such as:
foo();
I want to overload this function to have a default argument such as:
foo(bool verbose=false);
does this change force me to recompile every code that uses this function?
can't a call to foo()
without the argument keep working as the no-args-signature didn't change?
by the way - I'm using gcc
thanks
does this change force me to recompile every code that uses this function?
Yes, and the compile will fail, since there will be an ambiguity.
What you can do is overload the function like so:
foo(bool verbose);
and treat the case foo()
as if the parameter was false.
This wouldn't require a re-compilation. You'd just have two functions:
foo() { foo(false); } //possibly
foo(bool verbose);
instead of one with a default paramter.
这篇关于c ++重载函数默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!