且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c++ - <未解析的重载函数类型>

更新时间:2023-11-10 19:34:16

pointer-to-member-function 的类型与 pointer-to-function 不同.

函数的类型根据是普通函数还是某个类的非静态成员函数而不同:

The type of a function is different depending on whether it is an ordinary function or a non-static member function of some class:

int f(int x);
the type is "int (*)(int)" // since it is an ordinary function

还有

int Mat::f2(int x);
the type is "int (Mat::*)(int)" // since it is a non-static member function of class Mat

注意:如果是Fred类的静态成员函数,其类型与普通函数相同:"int (*)(char,float)"

Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)"

在 C++ 中,成员函数有一个隐式参数指向对象(成员函数内的 this 指针).正常 C函数可以被认为具有不同的调用约定来自成员函数,所以它们的指针类型(指向成员函数的指针与指向函数的指针)不同并且不兼容. C++ 引入了一种新的指针类型,称为成员指针,只能通过提供对象调用.

In C++, member functions have an implicit parameter which points to the object (the this pointer inside the member function). Normal C functions can be thought of as having a different calling convention from member functions, so the types of their pointers (pointer-to-member-function vs pointer-to-function) are different and incompatible. C++ introduces a new type of pointer, called a pointer-to-member, which can be invoked only by providing an object.

注意:不要试图将指向成员函数的指针强制转换"为函数指针;结果是不确定的,可能是灾难性的.例如,指向成员函数的指针不需要包含相应函数的机器地址. 正如上次所说例如,如果您有一个指向常规 C 函数的指针,请使用***(非成员)函数,或静态(类)成员函数.

NOTE: do not attempt to "cast" a pointer-to-member-function into a pointer-to-function; the result is undefined and probably disastrous. E.g., a pointer-to-member-function is not required to contain the machine address of the appropriate function. As was said in the last example, if you have a pointer to a regular C function, use either a top-level (non-member) function, or a static (class) member function.

更多关于这个这里这里.