卓越的C ++项目36 [英] Exceptional C++ Item 36

查看:87
本文介绍了卓越的C ++项目36的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为班级作业的一部分,我们正在阅读Herb Sutter的Exceptional C ++书.

We are reading Herb Sutter's Exceptional C++ book as part of our class assignments.

我对理解typedef实际在做什么和解密有疑问 构造方式:第36项:

I have a question in understanding what the typedef is actually doing and deciphering how it is constructed: Item 36:

如果对有人来说,使用typefef的逻辑会很有帮助.

If would be a great help for someone to walk though the logic of the typefef.

class B
{
public:
   virtual ~B();
   void operator delete (void*, size_t) throw();
   void operator delete[] (void*, size_t) throw();
   void f(void*, size_t) throw();
}
class D : public B
{
public:
   void operator delete (void*) throw();
   void operator delete[] (void*) throw();
};

typedef void (B::*PMF)(void*, size_t);
PMF p1 = &B::f;
PMF p2 = &B::operator delete;

由于B类(PMF)中没有指向成员函数的指针, 什么是PMF以及如何创建?

Since there is no pointer to a member function in class B (PMF), what is PMF and how does it get created?

是否使typedef的返回类型无效? 是(void *和size_t)是typedef的参数吗?

Is void the return type of the typedef? are (void* and size_t) the arguments of the typedef?

谢谢

推荐答案

关键字typedef为现有类型声明别名.

The typedef keyword declares an alias for an existing type.

例如

typedef int integral;
integral i = 1;

typedef void (B::*PMF)(void*, size_t);为类型声明别名PMF:函数指针(即指向 void size_t )返回 void.

The line typedef void (B::*PMF)(void*, size_t); declares an alias PMF for a type: pointer to function (that is taking a pointer to void and size_t) returning void.

有关如何使用螺旋规则从复杂声明中解释类型的详细说明,请参见此链接:

See this link for a detailed explanation of how to interpret the type from a complex declaration using the spiral rule: The Clockwise/Spiral Rule

PMF是该类型的别名.它可以用来声明一个指向B成员,具有相同参数列表和返回类型的函数的指针.

PMF is an alias for the type. It can be used to declare a pointer to any function that is a member of B and has the same parameter list and return type.

PMF p1 = &B::f; // Declare p1 as a PMF and set it to point to address of B::f.
B b; // Create instance of B (needed as p1 points to a member function).
(b.*p1)(nullptr, 0); // Here p1 can be used as if it was a call to B::f.

这篇关于卓越的C ++项目36的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆