C++ '&' : 对绑定成员函数表达式的非法操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28050325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:54:29  来源:igfitidea点击:

'&' : illegal operation on bound member function expression

c++

提问by Jerry

This works when I try from a single cpp file with a main function,

当我尝试从具有主函数的单个 cpp 文件时,这有效,

 sprintf(smem_options ,
     "#transcode{vcodec=RV24}:smem{"
     "video-prerender-callback=%lld,"
     "no-time-sync},"
    , (long long int)(intptr_t)(void*)&cbVideoPrerender
);

How do I pass function arguments to sprintf within a class?

如何在类中将函数参数传递给 sprintf?

sprintf(smem_options ,
     "#transcode{vcodec=RV24}:smem{"
     "video-prerender-callback=%lld,"
     "no-time-sync},"
    , (long long int)(intptr_t)(void*)&cbVideoPrerender
);

The error message I get is: error C2276: '&' : illegal operation on bound member function expression

我得到的错误信息是:错误 C2276: '&' : 对绑定成员函数表达式的非法操作

回答by Jonathan Wakely

Assuming cbVideoPrerendereris a member function in the second example, you need to say &Foo::cbVideoPrerendererwhere Foois the class it is a member of.

假设cbVideoPrerenderer在第二个例子中是一个成员函数,你需要说明它所属的类&Foo::cbVideoPrerenderer在哪里Foo

But that will only be valid if it is a static member function. Non-static member functions are not like normal functions, and when you form a pointer-to-member-function with the &Foo::barsyntax the thing you get back cannot be converted to a void*(it is typically something twice as large as a pointer, as it contains information about the object type).

但这只有在它是静态成员函数时才有效。非静态成员函数不像普通函数,当你用&Foo::bar语法形成一个指向成员函数的指针时,你得到的东西不能转换为 a void*(它通常是指针的两倍大,因为它包含有关对象类型的信息)。

回答by James Kanze

What you're trying to do is conditionally supported behavior in C++11, and illegal in earlier versions, in both cases. You can't reliably convert a pointer to a function (member or otherwise) to a void*. (I've worked on systems where a pointer to a function was 32 bits, but a void*only 16.)

在这两种情况下,您尝试做的是 C++11 中有条件支持的行为,而在早期版本中则是非法的。您无法将指向函数(成员或其他)的指针可靠地转换为void*. (我曾研究过指向函数的指针是 32 位,但void*只有 16位的系统 。)

In practice, most compilers will (illegally, in pre-C++11) ignore the error for non-member functions. Posix requires that function pointers and data pointers be compatible, and they are under Windows as well. (Today: only of the systems where they weren't for me was an early Unix.) As for pointers to members: a pointer to a static member has a type compatible to a pointer to a function (and so will work in practice, if the compiler allows it), but a pointer to a non-static member has a completely different type, usually with a different size, and a different representation. About the only way you can reliably output one is as a series of byte values: put the address in a properly typed variable, take the address of that variable, convert it to unsigned char const*, then use "%02x"to output each byte.

实际上,大多数编译器会(在 C++11 之前的版本中非法)忽略非成员函数的错误。Posix 要求函数指针和数据指针兼容,它们在 Windows 下也是如此。(今天:只有那些不适合我的系统是早期的 Unix。)至于成员指针:指向静态成员的指针具有与指向函数的指针兼容的类型(因此在实践中会起作用,如果编译器允许),但是指向非静态成员的指针具有完全不同的类型,通常具有不同的大小和不同的表示形式。关于可靠输出一个的唯一方法是作为一系列字节值:将地址放入正确类型的变量中,获取该变量的地址,将其转换为 unsigned char const*,然后用于"%02x"输出每个字节。

But the real question is why you want to do this. There is nothing that you can reliably do with the value you output, regardlessly of how you output it.

但真正的问题是你为什么要这样做。无论您如何输出,您都无法可靠地处理输出的值。