Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何构造一个typedef到rmember方法_C++_Typedef - Fatal编程技术网

C++ 如何构造一个typedef到rmember方法

C++ 如何构造一个typedef到rmember方法,c++,typedef,C++,Typedef,Win7 cygwin gcc(gcc)4.8.2 64位(2000 C++) 我正在尝试为成员函数构造typedef: class MyClass { typedef int32_t (*func1)(class const * const name); typedef int32_t (func2)(class const * const name); public: int32_t memberFunc(class const * const name); } MyCla

Win7

cygwin gcc(gcc)4.8.2 64位(2000 C++)

我正在尝试为成员函数构造typedef:

class MyClass {
   typedef int32_t (*func1)(class const * const name);
   typedef int32_t (func2)(class const * const name);
public:
   int32_t memberFunc(class const * const name);
}
MyClass:: someFunc() {
func1 x, y;
x = &memberFunc; // error: address of non-static member illegal
y = memberFunc;  // error: conversion error

func2 z;
z = memberFunc; // error: invalid use of member function
}
我看过以前的文章,它们表明func1定义是正确的,赋值(x=&memberFunc)也是正确的——至少对于非成员函数是正确的。如果可以的话,我不想让memberFunc成为静态的。那么,有什么好的例子和解释吗

谢谢
art

函数指针无法绑定到成员函数。


您需要一个指向成员函数的指针,或者更易于使用的东西,如
std::function
和对
std::bind
的调用实际上,成员函数会收到另一个参数:
指针。所以普通函数指针和成员函数指针是不同的。成员函数poineter语法如下所示:

typedef int32_t (MyClass::*func1)(MyClass const * const name);
其用途如下:

func1 f = &MyClass::memberFunc;

MyClass c;
MyClass *pc = &c;
(c.*f)(nullptr);
(pc->*f)(nullptr);
MyClass c;
auto f = std::bind(&MyClass::memberFunc, &c /*this pointer*/);

PS.
class const*const name
要使其工作,您需要一种称为a的方法

实际上,函数的地址不足以调用非静态类函数,因为实例化的对象具有非本地存储。因此,要真正调用成员函数,还需要一个
this
指针


其他地方已经解释过了,但我想我会加入技术术语。任何包装函数地址和必要参数(即
this
指针)以满足类函数调用约定的东西都是闭包的实现。

查看带有
y
错误消息的闭包,看看它需要哪种类型。对于
x
,它应该是
&MyClass::memberFunc
,然后您可能会得到同样的结果。我不会详细介绍,因为这个主题以前已经讨论过很多关于堆栈溢出的内容。这上面有上百个问题。