Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The .* operator is the C++ pointer-to-member access operator used to bind a pointer-to-member to a specific object instance. It evaluates to the actual member (data or function) of the left-hand object designated by the right-hand pointer-to-member.
object.*pointer_to_member

Operands and Type Requirements

  • Left Operand: Must be an object of class type T (or a class derived from T, provided T is an unambiguous and accessible base class of the derived class). It can be an lvalue or an rvalue.
  • Right Operand: Must be a pointer-to-member of class T. It cannot be a standard pointer; it must be declared using the Type T::* syntax.

Pointer-to-Data-Member Mechanics

When the right operand is a pointer to a data member, the .* operator computes the memory offset of that member relative to the base address of the left operand. The value category of the result depends on the left operand:
  • If the object is an lvalue, the result is an lvalue.
  • If the object is an rvalue, the result is an xvalue (expiring value).
  • The cv-qualification (const/volatile) of the result is the union of the cv-qualifiers of the left operand and the type pointed to by the pointer-to-member. Because a pointer-to-member does not retain the mutable property of a data member, accessing a mutable member of a const object via .* yields a const result (unlike direct . member access).
struct Entity {
    int x;
};

int Entity::* member_ptr = &Entity::x;

Entity obj;
obj.*member_ptr = 10; // Evaluates to an lvalue of type int designating obj.x

Pointer-to-Member-Function Mechanics

When the right operand is a pointer to a member function, the .* operator yields a special, un-nameable bound member function entity. This entity has strict syntactic limitations: it must be immediately invoked using the function call operator (). It cannot be assigned to a variable, cast, or passed as an argument. Because the function call operator () has higher precedence than the .* operator, the entire .* expression must be enclosed in parentheses.
struct Entity {
    void execute(int val) {}
};

void (Entity::* func_ptr)(int) = &Entity::execute;

Entity obj;
(obj.*func_ptr)(42); // Parentheses around (obj.*func_ptr) are mandatory

Operator Characteristics

  • Precedence: The .* operator has a precedence level of 4, which is lower than the member access operator . and the function call operator (), but higher than multiplication *.
  • Associativity: Left-to-right.
  • Overloadability: The .* operator cannot be overloaded. This guarantees that its semantics regarding offset calculation and member binding remain strictly defined by the compiler. (Note: Its counterpart, ->*, can be overloaded).
  • Polymorphism: If the right operand points to a virtual member function, the .* operator respects dynamic dispatch. The actual function invoked will be resolved at runtime based on the dynamic type of the left operand.
Master C++ with Deep Grasping Methodology!Learn More