禁用“错误函数转换”警告
我收到以下警告:
warning: converting from 'void (MyClass::*)(byte)' to 'void (*)(byte)'
这是因为我需要传递成员函数而不是普通函数作为参数。但程序运行正确。
我想禁用此警告(Wno-bad-function-cast 不适用于 C++)或实现不同的方式来传递成员函数。
I'm receiving the following warning:
warning: converting from 'void (MyClass::*)(byte)' to 'void (*)(byte)'
This is because I need to pass as argument a member function instead of an ordinary function. But the program is running correctly.
I'd like to disable this warning (Wno-bad-function-cast doesn't work for C++) or to implement a different way to pass a member function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。认真对待这个警告。您应该更改代码来处理这种情况。
指向成员函数的指针(
void (MyClass::*)(byte)
)和普通函数指针(void (*)(byte)
)完全不同。 查看此链接。你不能就这样施放它们。它会导致未定义的行为或崩溃。看这里,它们有何不同:
现在您可能会觉得,
foo(byte)
和MyClass::foo(byte)
具有相同的签名,那么为什么它们的函数指针是不一样。这是因为,MyClass::foo(byte)
在内部被解析为某种程度上,现在您可以闻到它们之间的区别。
将指向成员函数的指针声明为,
您必须将此
ptr
与MyClass
的对象一起使用,例如:No. Take this warning seriously. You should rather change your code to handle this scenario.
Pointer to member function(
void (MyClass::*)(byte)
) and normal function pointer (void (*)(byte)
) are entirely different. See this link. You cannot cast them just like that. It results in undefined behavior or crash.See here, how they are different:
Now you may feel that,
foo(byte)
andMyClass::foo(byte)
have same signature, then why their function pointers are NOT same. It's because,MyClass::foo(byte)
is internally resolved somewhat as,Now you can smell the difference between them.
Declare pointer to member function as,
You have to use this
ptr
with the object ofMyClass
, such as:您无法将带有两个参数的函数传递到需要带有一个参数的函数的位置。做不到,算了,故事结束了。调用者将一个参数传递给您的函数。它不知道第二个参数,它不会将其传递给您的函数,无论您如何努力,您都无法让它执行您想要的操作。
出于同样的原因,您不能在需要常规函数的地方传递非静态成员函数。成员函数需要一个对象来操作。无论调用函数的代码都不知道该对象,都无法将对象传递给它,也无法使其使用考虑该对象的正确调用序列。
获取用户函数而不获取用户可能想要传递给其函数的附加数据的接口本质上是邪恶的。查看 C 标准库中的
qsort()
函数。这是邪恶界面的一个例子。假设您想根据外部定义的某种排序规则对字符串数组进行排序。但它接受的只是一个带有两个值的比较函数。如何将该排序规则传递给比较函数?你不能,所以如果你想让它工作,你必须使用一个邪恶的全局变量,并附加所有的字符串。这就是为什么 C++ 不再传递函数指针,而是转向函数对象。函数对象可以封装任何你想要的数据。
You can't pass a function that takes two arguments to a place that expects a function that takes one. Can't be done, forget about it, period, end of story. The caller passes one argument to your function. It doesn't know about the second argument, it doesn't pass it to your function, you can't make it do what you want however hard you try.
For the very same reason you can't pass a non-static member function where a regular function is expected. A member function needs an object to operate on. Whatever code calls your function doesn't know about the object, there's no way to pass it the object, and there's no way to make it use the right calling sequence that takes the object into account.
Interfaces that take user's functions, without taking additional data that the user might want to pass to his function, are inherently evil. Look at the
qsort()
function from the C standard library. That's an example of an evil interface. Suppose you want to sort an array of string according to some collation scheme defined externally. But all it accepts is a comparison function that takes two values. How do you pass that collation scheme to your comparison function? You can't, and so if you want it working, you must use an evil global variable, with all the strings attached to it.That's why C++ has moved away from passing function pointers around, and towards function objects. Function objects can encapsulate whatever data you want.
另外,这可能会有所帮助
Also, this may be helpful