[英]Function pointers with default parameters in C++
How does C++ handle function pointers in relation to functions with defaulted parameters? C ++如何处理与具有默认参数的函数相关的函数指针?
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()
and bar()
can only be assigned to func_ptr2
. foo()
和bar()
都只能分配给func_ptr2
。
§8.3.6/2 : §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]
- 末端示例]
无法为函数指针提供默认参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.