在C ++中使用默认参数的函数指针 [英] Function pointers with default parameters in C++
本文介绍了在C ++中使用默认参数的函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C ++如何处理与默认参数相关的函数指针?
如果我有:
void foo f = 0.0f);
和
void bar(int i,float f);
void(* func_ptr1)(int);
void(* func_ptr2)(int,float);
void(* func_ptr3)(int,float = 10.0f);哪些函数指针可以用于哪个函数?
foo()bar()
只能分配给func_ptr2
。
§8.3.6/ 2 :
默认参数不是函数类型的一部分。 [示例:
int f(int = 0)
void h(){
int j = f(1);
int k = f(); // OK,means f(0)
}
int(* p1)(int)=& f;
int(* p2)()=& f; //错误:类型不匹配
How does C++ handle function pointers in relation to functions with defaulted parameters?
If I have:
void foo(int i, float f = 0.0f); void bar(int i, float f); void (*func_ptr1)(int); void (*func_ptr2)(int, float); void (*func_ptr3)(int, float = 10.0f);
Which function pointers can I use in relation to which function?
解决方案Both
foo()
andbar()
can only be assigned tofunc_ptr2
.§8.3.6/2:
A default argument is not part of the type of a function. [Example:
int f(int = 0); void h() { int j = f(1); int k = f(); // OK, means f(0) } int (*p1)(int) = &f; int (*p2)() = &f; // error: type mismatch
--end example]
这篇关于在C ++中使用默认参数的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文