带成员函数指针的typedef语法

时间:2021-11-05 22:30:23

according to MSDN the typedef syntax is:

根据MSDN,typedef语法是:

typedef type-declaration synonym;

typedef type-declaration同义词;

Very easy:

typedef int MY_INT;

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

但是成员函数指针typedef如何符合这条规则呢?

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的reverse / random / front / last / mixed语法的逻辑步骤是什么?

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

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

10 个解决方案

#1


31  

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 int (*FuncPtr)(int); 

Simple! The only difference is, in the typedef FuncPtr becomes a type, while in the pointer declaration, FuncPtr is 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!

#2


10  

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

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

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

#3


9  

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);

#4


4  

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)。或者玩宣言,看看你是否得到了你想要的东西。

#5


3  

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中添加返回类型,用类型和参数替换变量。

#6


1  

I once read a nice explanation (but it's from Expect C Programming so 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.

实际上,typedef具有与变量声明完全相同的格式,只有这个额外的关键字才能提示您。

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关键字不会创建变量,而是使声明说“此名称是所声明类型的同义词”。

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

所以你有它。想象一下,你正在声明一个变量,在它之前粘贴typedef,瞧,你有一个新类型。 MSDN的解释很复杂:我读过真正的上帝和坏的。

#7


1  

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”可以在大多数有效声明之前使用。

#8


1  

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

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

  • in declaration, use "A::" as prefix
  • 在声明中,使用“A ::”作为前缀

  • when use it, use "a." as prefix
  • 使用时,请使用“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;
}

#9


0  

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.

希望它减少混乱。

#10


0  

Here's a complete and accurate description: http://tipsandtricks.runicsoft.com/Cpp/MemberFunctionPointers.html

以下是完整而准确的说明:http://tipsandtricks.runicsoft.com/Cpp/MemberFunctionPointers.html

#1


31  

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 int (*FuncPtr)(int); 

Simple! The only difference is, in the typedef FuncPtr becomes a type, while in the pointer declaration, FuncPtr is 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!

#2


10  

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

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

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

#3


9  

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);

#4


4  

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)。或者玩宣言,看看你是否得到了你想要的东西。

#5


3  

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中添加返回类型,用类型和参数替换变量。

#6


1  

I once read a nice explanation (but it's from Expect C Programming so 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.

实际上,typedef具有与变量声明完全相同的格式,只有这个额外的关键字才能提示您。

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关键字不会创建变量,而是使声明说“此名称是所声明类型的同义词”。

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

所以你有它。想象一下,你正在声明一个变量,在它之前粘贴typedef,瞧,你有一个新类型。 MSDN的解释很复杂:我读过真正的上帝和坏的。

#7


1  

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”可以在大多数有效声明之前使用。

#8


1  

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

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

  • in declaration, use "A::" as prefix
  • 在声明中,使用“A ::”作为前缀

  • when use it, use "a." as prefix
  • 使用时,请使用“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;
}

#9


0  

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.

希望它减少混乱。

#10


0  

Here's a complete and accurate description: http://tipsandtricks.runicsoft.com/Cpp/MemberFunctionPointers.html

以下是完整而准确的说明:http://tipsandtricks.runicsoft.com/Cpp/MemberFunctionPointers.html