。*&是什么?接线员呢? [英] What does the .*& operator do?

查看:78
本文介绍了。*&是什么?接线员呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了这个问题,其中的答案使用了奇怪的构造:

I stumbled across this question, which had an answer that used an odd construct:

typedef std::queue<int> Q;
typedef Q::container_type C;

C & get (Q &q)
{
    struct hack : private Q {
        static C & get (Q &q) {
            return q.*&hack::c;
        }
    };
    return hack::get(q);
}

我通常会遵循 q 可以访问由 get 函数引用的自己的 c 成员。但是,我不知所措以明确地解释它。 。*& 到底发生了什么,为什么允许它?

I generally follow that q has access to its own c member that is being referenced by the get function. But, I am at a loss to clearly explain it. What is happening exactly with the .*&, and why is it allowed?

推荐答案

typedef std::queue<int> Q;

Q 队列适应的容器。

typedef Q::container_type C;

C Q -这是 deque< int>

C & get (Q &q) {

获取接受队列并返回 deque 。实际上,它返回 queue 包裹的 deque :通过传统方式,这是不可能的。

get takes a queue and returns a deque. In fact it returns the deque that the queue wraps: by conventional means, this is not possible.

  struct hack : private Q {

hack 是该函数的本地类型。它从 Q 继承,并且只有一个静态成员函数。从它的名字,您可能会怀疑它是一个hack。您是对的。

hack is a type local to the function. It inherits from Q and has only one static member function. From its name, you may suspect it is a hack. You are right.

没有实例化 hack

    static C & get (Q &q) {

hack :: get get 本身具有相同的签名。实际上,我们将 get 的所有工作委托给此方法。

hack::get has the same signature as get itself. In fact we delegate all of the work of get to this method.

      return q.*&hack::c;

此行需要细分。我将在更多行中这样做:

this line needs to be broken down. I will do it in more lines:

      using mem_ptr_t = C Q::*; // aka typedef C Q::*mem_ptr_t;
      mem_ptr_t c_mem_ptr = &hack::c;
      C& ret = q.*c_mem_ptr;
      return ret;

第一行定义指向类型字段的成员指针的类型 Q 中的C 。 C ++ 11和C ++ 03命名此类型的方法都很丑陋。

The first line defines the type of a member pointer to a field of type C within a Q. Both the C++11 and C++03 ways of naming this type are ugly.

第二行获取指向字段的成员指针 Q 中的c 。它是通过C ++类型系统中的漏洞来完成的。 &:hack :: c 在逻辑上是 C hack :: * -指向类型成员的指针 hack 类型的类中的 C 。实际上,这就是为什么我们可以在 hack static 成员中访问它的原因。但是有问题的 c 实际上在 Q 中,因此C ++中表达式的实际类型是 CQ :: * :指向 Q 成员变量的指针。

The second line gets a member pointer to the field c in Q. It does this through the hole in the type system of C++. &hack::c is logically of type C hack::* -- a pointer to a member of type C within a class of type hack. In fact, that is why we can access it in a static member of hack. But the c in question is actually in Q, so the actual type of the expression in C++ is C Q::*: a pointer to a member variable of Q.

您不能在 hack 内直接获取该成员指针-& Q :: c 是非法的,但& hack :: c 不是。

You cannot directly get this member pointer within hack -- &Q::c is illegal, but &hack::c is not.

您可以将成员指针视为另一种类型的类型化偏移量: & hack :: c Q c 的偏移量 c $ c>并知道其类型为 C 。现在这不是真的-它是一个不透明的值,告诉编译器如何从 Q 中获取 c -但这有助于以这种方式考虑(并且在简单情况下也可以以这种方式实现)。

You can think of member pointers as 'typed offsets' into another type: &hack::c is the "offset" of c within Q together with knowing it is of type C. Now this isn't really true -- it is some opaque value that tells the compiler how to get c from Q -- but it helps to think about it that way (and it may be implemented that way in simple cases).

然后,我们将该成员指针与 Q& Q 中获得 c 。获取成员指针受到保护的约束:使用它不是!我们的方法是使用运算符。* ,它是成员取消引用运算符,您可以在其上传递成员函数指针成员。

We then use this member pointer together with a Q& to get the c out of the Q. Getting a member pointer is constrained by protected: using it is not! The way we do it is with operator .*, which is the member dereference operator, which you can pass either member function pointers or members on the right, and class instances on the left.

instance。* member_ptr 是一个查找成员指向的表达式在实例中由 member_ptr 进行。在原始代码中,所有操作都在一行上完成:

instance .* member_ptr is an expression that finds the member "pointed to" by member_ptr within the instance. In the original code, everything was done on one line:

instance .* &class_name::member_name

,所以看起来好像有一个运算符。*&

so it looked like there was an operator .*&.

    }
  };

然后我们关闭静态方法并 hack 类,并且:

and then we close up the static method and hack class, and:

  return hack::get(q);
}

称呼它。通过此技术,可以访问受保护的状态:没有它,只能在同一实例的子类中访问受保护的成员。 。使用此方法,我们可以访问 any 实例的受保护的成员,而不会违反任何标准。

call it. This technique gives access to protected state: without it, protected members can only be accessed in child classes of the same instance. Using this, we can access protected members of any instance, without violating any bit of the standard.

这篇关于。*&amp;是什么?接线员呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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