C++ 带有成员函数指针的 typedef 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6265851/
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 19:49:08  来源:igfitidea点击:

typedef syntax with member function pointers

c++typedef

提问by muxmux

according to MSDN the typedef syntax is:

根据 MSDN,typedef 语法是:

typedef type-declaration synonym;

typedef 类型声明同义词;

Very easy:

好简单:

typedef int MY_INT;

But how the heck does the member-function-pointer typedefs comply to this rule?

但是成员函数指针类型定义到底是如何遵守这个规则的呢?

typedef int (MyClass::*MyTypedef)( int);

100% confusion – the synonym (MyTypedef) is in the middle?

100% 混淆——同义词 ( MyTypedef) 在中间?

Can someone please explain what the logical steps are to get from the very easy to understand syntax format of MSDN to the reverse/random/front/last/mixed syntax thing of aboves typedef?

有人可以解释一下从 MSDN 的非常容易理解的语法格式到上面 typedef 的反向/随机/前面/最后/混合语法的逻辑步骤是什么吗?

*edit thanks for all the fast answers (and the beautification of my post) :)

*编辑感谢所有快速答案(以及我的帖子的美化):)

回答by Nawaz

the synonym (MyTypedef) is in the middle??

同义词(MyTypedef)在中间??

Its not in the middle. Just forget member-function for a while, see how a function pointer is defined:

它不在中间。暂时忘记成员函数,看看函数指针是如何定义的:

int (*FuncPtr)(int);

And this is how you would typedef it:

这就是你如何对它进行 typedef:

typedef int (*FuncPtr)(int); 

Simple! The only difference is, in the typedef FuncPtrbecomes a type, while in the pointer declaration, FuncPtris a variable.

简单的!唯一的区别是,在 typedef 中FuncPtr变成了类型,而在指针声明中,FuncPtr则是一个变量

Similarly,

相似地,

int (MyClass::*MyTypedef)( int); //MyTypedef is a variable

And the typedef as:

和 typedef 为:

typedef int (MyClass::*MyTypedef)( int); //MyTypedef is a type!

回答by kfsone

It's worth noting that, as of C++11, you could write this expression as a more legible usingstatement:

值得注意的是,从 C++11 开始,您可以将此表达式编写为更清晰的using语句:

using MyTypedef = int (MyClass::*)(int);

回答by ybungalobill

How do you define a pointer to member function? Like this:

如何定义指向成员函数的指针?像这样:

int (A::*variableName)(int);

To make it a typedef, just add a typedef:

要使其成为 typedef,只需添加一个 typedef:

typedef int (A::*typedefName)(int);

回答by zhanxw

The syntax of using member function pointer has to (assume ais an instance of class A):

使用成员函数指针的语法必须(假设a是 class 的一个实例A):

  • in declaration, use "A::" as prefix
  • when use it, use "a." as prefix
  • 在声明中,使用“A::”作为前缀
  • 使用时,用“a”。作为前缀

Below is a toy example. You can play with it.

下面是一个玩具示例。你可以玩它。

#include <stdio.h>
#include <stdlib.h>
class A;
typedef int (A::*F)(double);

class A {
 public:
  int funcDouble(double x) { return (int)(x * 2.0); }
  int funcTriple(double x) { return (int)(x * 3.0); }

  void set(int a) {
    if (a == 2) {
      this->f_ = &A::funcDouble;
    } else if (a == 3) {
      this->f_ = &A::funcTriple;
    } else {
      this->f_ = NULL;
    }
  }

 public:
  F f_;
};
int main(int argc, char *argv[]) {
  A a;
  a.set(2);
  F f = &A::funcDouble;
  printf("double of 1 = %d\n", (a.*f)(1));

  // Below is equivalent to:
  // F f2 = a.f_;
  // printf("double of 1 = %d\n", (a.*f2)(1));
  printf("double of 1 = %d\n", (a.*(a.f_))(1));

  a.set(3);
  printf("triple of 1 = %d\n", (a.*(a.f_))(1));

  return 0;
}

回答by AProgrammer

The principle for declaration in C++ is that they mimick the use. If you want to use a pointer to member function pmf, you'll write:

C++中声明的原则是模仿使用。如果你想使用指向成员函数 pmf 的指针,你会写:

(myVar.*pmf)(arg);

so to define a typedef for it, you write:

所以要为它定义一个 typedef,你写:

typedef int (MyClass::*pmf)(int);

adding the return type in head, replacing the variable by the type and arguments by their type.

在 head 中添加返回类型,用类型替换变量,用它们的类型替换参数。

回答by davka

I know you've already got your answer, but want to share this - it's handy: http://www.cdecl.org. It's a C/C++ declaration <-> English translator. Just type in

我知道你已经得到了答案,但想分享这个 - 它很方便:http: //www.cdecl.org。这是一个 C/C++ 声明 <-> 英文翻译器。只需输入

declare x as pointer to member of class A function (int) returning char

将 x 声明为指向 A 类函数 (int) 成员的指针,返回 char

and you get char (A::*x)(int ). Or play around with the declaration and see if you get what you want.

你得到char (A::*x)(int ). 或者玩弄声明,看看你是否得到了你想要的。

回答by cnicutar

I once read a nice explanation (but it's from Expect C Programmingso I ymmv):

我曾经读过一个很好的解释(但它来自Expect C Programming所以我 ymmv):

In fact, a typedef has exactly the same format as a variable declaration, only with this extra keyword to tip you off.

Since a typedef looks exactly like a variable declaration, it is read exactly like one. Instead of the declaration saying "this name refers to a variable of the stated type," the typedef keyword doesn't create a variable, but causes the declaration to say "this name is a synonym for the stated type."

事实上,typedef 的格式与变量声明的格式完全相同,只是使用了这个额外的关键字来提示您。

由于 typedef 看起来与变量声明完全一样,因此它的读取方式与变量声明完全一样。typedef 关键字并没有创建变量,而是导致声明说“这个名称是所述类型的同义词”,而不是声明说“这个名称指的是指定类型的变量”。

So there you have it. Imagine you're declaring a variable, stick typedefbefore it and voila, you have a new type. MSDNexplanations are a mixed bag: I've read really god ones and downright bad ones.

所以你有它。想象一下,你正在声明一个变量,typedef在它前面贴上,瞧,你有了一个新类型。MSDN解释是混合包:我读过真正的上帝和彻头彻尾的坏的。

回答by Nick

The page you are referring to is probably "typedef Specifier". The simplistic "typedef type-declaration synonim;" syntax is just one of the many ways to use typedef. There is (probably) no simple and concise way to describe how typedef can be used. That is what the "Typedef Declarations" MSDN page is for.

您所指的页面可能是“ typedef Specifier”。简单的“ typedef type-declaration synonim;”语法只是使用 typedef 的众多方法之一。没有(可能)简单简洁的方法来描述如何使用 typedef。这就是“ Typedef 声明”MSDN 页面的用途。

You will notice on this page something like:

您会在此页面上注意到以下内容:

declaration: 
    declaration-specifiers init-declarator-list opt ; 
declaration-specifiers: 
    storage-class-specifier declaration-specifiers opt 
    type-specifier declaration-specifiers opt
    type-qualifier declaration-specifiers opt 
storage-class-specifier: 
    typedef

More details as to what declaration-specifiers and init-declarator-list can be found here.

可以在此处找到有关声明说明符和 init-declarator-list 的更多详细信息。

This is one rigorous way to understand all possible usages for "typedef".

这是理解“typedef”所有可能用法的一种严格方法。

What this page basically says is that "typedef" can be used before most valid declarations.

这个页面基本上说的是“typedef”可以在大多数有效声明之前使用。

回答by Felipe Sodre Silva

It will be easier to understand when you start thinking like this:

当你开始这样思考时会更容易理解:

Whenever you see a function like this:

每当你看到这样的函数时:

TYPE foo(int arg1, int arg2);

You say that the type of foo is TYPE. So, the type of

你说 foo 的类型是 TYPE。所以,类型

int get_next_prime();

is int.

是整数。

You can see that when you pass a function pointer as an argument to a function:

您可以看到,当您将函数指针作为参数传递给函数时:

void register_callback(void (*ptr)(int));

In this case, you are passing a function of type void as an argument.

在这种情况下,您将一个 void 类型的函数作为参数传递。

Now, when you see something like that:

现在,当你看到类似的东西时:

typedef int (A::*typedefName)(int);

you are just saying that the variable (A::*typedefName)(int) (this is only one thing, not two, since it's a function pointer declaration) is actually of type int. From then on, the compiler will interpret A::*typedefName as a function of type int, that is, it returns an int value.

你只是说变量 (A::*typedefName)(int) (这只是一件事,而不是两件事,因为它是一个函数指针声明)实际上是 int 类型。之后,编译器会将 A::*typedefName 解释为 int 类型的函数,即返回一个 int 值。

Hope it makes it less confusing.

希望它让它不那么令人困惑。