Files
cppdraft_translate/cppdraft/dcl/mptr.md
2025-10-25 03:02:53 +03:00

4.1 KiB
Raw Blame History

[dcl.mptr]

9 Declarations [dcl]

9.3 Declarators [dcl.decl]

9.3.4 Meaning of declarators [dcl.meaning]

9.3.4.4 Pointers to members [dcl.mptr]

1

#

The component names of a ptr-operator are those of its nested-name-specifier, if any.

2

#

In a declarationTD whereD has the form

nested-name-specifier * attribute-specifier-seqopt cv-qualifier-seqopt D1

and thenested-name-specifier designates a class, and the type of the contained declarator-id in the declarationTD1 is “derived-declarator-type-list€, the type of the declarator-id inD is “derived-declarator-type-list cv-qualifier-seq pointer to member of classnested-name-specifier of typeT”.

The optional attribute-specifier-seq ([dcl.attr.grammar]) appertains to the pointer-to-member.

The nested-name-specifier shall not designate an anonymous union.

3

#

[Example 1:

struct X {void f(int); int a;};struct Y;

int X::* pmi = &X::a;void (X::* pmf)(int) = &X::f;double X::* pmd;char Y::* pmc; declarespmi,pmf,pmd andpmc to be a pointer to a member ofX of typeint, a pointer to a member ofX of typevoid(int), a pointer to a member ofX of typedouble and a pointer to a member ofY of typechar respectively.

The declaration ofpmd is well-formed even thoughX has no members of typedouble.

Similarly, the declaration ofpmc is well-formed even thoughY is an incomplete type.

pmi andpmf can be used like this:X obj;// ... obj.*pmi = 7; // assign 7 to an integer member of obj(obj.*pmf)(7); // call a function member of obj with the argument 7

— end example]

4

#

A pointer to member shall not point to a static member of a class ([class.static]), a member with reference type, or “cv void”.

5

#

[Note 1:

See also [expr.unary] and [expr.mptr.oper].

The type “pointer to member” is distinct from the type “pointer”, that is, a pointer to member is declared only by the pointer-to-member declarator syntax, and never by the pointer declarator syntax.

There is no “reference-to-member” type in C++.

— end note]