CKTIS / C++
Forming pointers to members in C++
`&C::m` can identify a non-static member without choosing an object. Apply the resulting member pointer to an object or use it as a projection in a ranges algorithm.
Version: ISO C++23; implementation support may vary
Syntax
T C::* pm = &C::m;
R (C::* pmf)(Args...) const = &C::f;
obj.*pm
ptr->*pm
(obj.*pmf)(args...)
(ptr->*pmf)(args...)In C++, pointer-to-member formation uses the built-in unary & directly before an unparenthesized qualified name, as in &C::m. Neither &(C::m), bare C::m, nor unqualified &m forms a member pointer. Parentheses around the whole expression, (&C::m), are fine. The member must be accessible, and you cannot form a pointer to a bit-field.
For a non-static int data member C::m, the type of &C::m is int C::*: it identifies which member to select, not a particular object or a portable byte offset. The class in the resulting member pointer type is the class that declares the member, not necessarily the class used to qualify its name. If D inherits m from C and D::m names that inherited member, &D::m also has type int C::*. For an existing C object obj, &obj.m has type int* and points to that object's member. The address of a static int member also has type int*.
For an implicit-object member function int C::f(int) const, the pointer type is int (C::*)(int) const. If f is overloaded, int (C::*pf)(int) const = &C::f; supplies a target type that must select one function unambiguously. Taking the address of a static member function or an explicit-object member function (this parameter, C++23) gives an ordinary function pointer instead.
Use obj.*pm with an object or ptr->*pm with an object pointer; call a member function as (obj.*pf)(arg) or (ptr->*pf)(arg). The object must be of the appropriate class or of a derived class with an accessible, unambiguous base of that type. Member access is also possible during construction and destruction, subject to [class.cdtor]. For example, a constructor body can read an already initialized data member through a member pointer before the complete object's lifetime begins. Applying a null member pointer (nullptr) is undefined behavior.
In std::ranges::sort(rows, std::ranges::less{}, &Row::key), the member pointer is a projection: the algorithm compares key values but reorders whole records. It does not need a comparison defined for whole Row objects.
Examples
#include <algorithm>
#include <functional>
#include <iostream>
struct Row {
char label;
int key;
int value() const { return key; }
int value(int factor) const { return key * factor; }
};
int main() {
int Row::* key = &Row::key;
int (Row::* read)(int) const = &Row::value;
Row row{'A', 20};
Row* ptr = &row;
int* address = &row.key;
std::cout << (row.*key) << ' ' << *address << ' '
<< (ptr->*read)(2) << '\n';
Row rows[]{row, {'B', 10}, {'C', 30}};
std::ranges::sort(rows, std::ranges::less{}, key);
for (const Row& item : rows) {
std::cout << item.label << ':' << item.key << '\n';
}
}Result
20 20 40 B:10 A:20 C:30
The first two values are both `20`: one comes through the member pointer and the other through the address of a specific member. The selected overload `value(int) const` returns `40`. The whole records then appear in `B`, `A`, `C` order, by ascending `key`.
Related lessons
- C++ · Lesson 28 · Advanced · Version: 3
- C++ · Lesson 35 · Advanced · Version: 3
Sources
- C++23 working draft N4950 — [expr.unary.op], [expr.mptr.oper], [class.cdtor], [over.over], [func.require], [sort]
- Unary operators [expr.unary.op]
- Pointers to members [dcl.mptr]
- Address of an overload set [over.over]
- Pointer-to-member operators [expr.mptr.oper]
- Construction and destruction [class.cdtor]
- Function object requirements [func.require]
- sort [sort]