在标准中(例如N3337- 不是最新的,但很好)第 8.3.3.3 节末尾有一条注释,内容如下:
[注:另见 5.3 和 5.5。 “指向成员的指针”类型是不同的
来自“指针”类型,即仅声明指向成员的指针
通过指向成员声明符语法的指针,而不是通过指针
声明符语法。 C++ 中没有“成员引用”类型。 —
尾注]
另外,当然,不存在“对成员的引用”类型运算符(假设,我能想到的最好的方法是->&
and .&
,尽管这些与数据和函数引用的取消引用不一致,这不需要特殊的运算符)。
Why?
至于为什么;经过一次有趣的小历史调查并未能找到任何现有的注释(我一路回到前端2.0其中首次支持成员指针罢工> --编辑:根据一份更可信的文件,该功能实际上是在 Cfront 1.2 中首次支持的),我问了这个人本人,他的答复如下:
Date: Sat, 22 Feb 2014 10:12:51 -0500
From: Bjarne Stroustrup <...>
Subject: Re: On lack of reference-to-member and CFront 2.0
On 2/22/2014 6:40 AM, Jason C wrote:
> My question is: C++ very clearly disallows the concept of
> "reference-to-member". Why is this? I have been doing a lot of
> research, and I traced the origin of "pointer-to-member" back (I
> think) to 1989 CFront 2.0. I read through the product reference manual
> and other documentation hoping to find an explanation of some sort but
> could not.
I don't really remember. It was 25+ years ago and the ARM is silent on
this. I added pointers to members to eliminate the need for a rare
breakage of the type system. I suspect that I didn't add references to
members because it did not seem worth the effort: there was no use case.
老实说,我期待的是更加神秘和复杂的事情。
所以你明白了:下次有人问为什么没有对成员的引用时,你可以自信地说,“因为没有!”(注:请参阅我在评论中的胡言乱语;仍需进行一些历史调查才能获得 100% 的置信度。)
就我个人而言,我从未在自己的代码中发现过成员指针的用途,但 Stroustrup 给出了它们存在的明显理由C++ 的演变:1985-1989,第 222-223 页。
顺便说一下,调用假设的成员函数引用的语法:
object.method();
...没有多大意义,因为没有办法在语法上区分它与对名为的实际成员的调用method()
.
hvd 在下面提出了一个很好的观点:正如您从上面看到的,从语法上讲,实际上并没有一种一致的方法来取消引用对成员的引用。您必须将其与普通成员访问区分开来,但同时您希望使其与对象和函数引用的取消引用(不需要特殊运算符)保持一致,而且我真的想不出有什么可以实现这两者。