你可以有一个“指向成员的指针”到工会的成员? [英] Can you have a "pointer to member" to a member of a union?
问题描述
在类的上下文中,我可以找到的所有描述都涉及指向成员的指针。工会与结构非常相似,特别是有成员。
union x {
int a;
float b;
};
int x :: * p =& x :: a;
我不是指向联合的指针,指向联合的成员,等等。 p
在上面的例子中真的是一个偏移量,显然是大小为0.我需要这个构造来回答此问题。
§3.9.2/ 1:复合类型可以通过以下方式构造:...指向非静态50类成员的指针,它标识给定类的对象内给定类型的成员, p>
§8.3.3/ 1:在声明TD中,D的格式为...,而且 nested-name-specifier / p>
§5.3.1/ 3:一元&
运算符的结果是指向其操作数的指针。操作数应为左值或 qualified-id 。如果操作数是一个 T
,结果具有类型指向类型T的类C的成员的指针,并且是指定 C :: m
。
当然,§9.5/ 5:是 c $ c> union ...
(§3.9.2/ 1也提到:联盟 ...)
没有提到类不能是联合,所以是的,你可以形成这样的PTM类型和值。
All descriptions that I can find talk about "pointer to member" in the context of a class. Unions are very similar to structures, and in particular have members too. Can you have a pointer to those members too?
E.g.
union x {
int a;
float b;
};
int x::*p = &x::a;
I'm not talking about pointer to the union as a whole, pointers as members of a union, etc. p
in the example above would really be an offset, obviously of size 0. I'd need this construct to answer this question.
§3.9.2/1: Compound types can be constructed in the following ways: … pointers to non-static 50 class members, which identify members of a given type within objects of a given class,
§8.3.3/1: In a declaration T D where D has the form … and the nested-name-specifier denotes a class, …
§5.3.1/3: The result of the unary &
operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. If the operand is a qualified-id naming a non-static member m
of some class C
with type T
, the result has type "pointer to member of class C of type T" and is a prvalue designating C::m
.
And of course §9.5/5: A union is a class defined with the class-key union
…
(§3.9.2/1 also mentions: unions, which are classes…)
No mention that the class cannot be a union, so yes, you can form such a PTM type and value.
这篇关于你可以有一个“指向成员的指针”到工会的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!