> ## 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.

# C++ Pointer to Data Member

A pointer to a data member is a specialized C++ construct that stores the relative offset of a non-static data member within a class layout, rather than an absolute memory address. It acts as a typed offset, requiring an actual object instance of the class to be dereferenced in order to resolve to a concrete memory location and yield a value.

## Declaration Syntax

To declare a pointer to a data member, the scope resolution operator `::` is combined with the pointer asterisk `*` to bind the pointer to a specific class type.

```cpp theme={"dark"}
DataType ClassName::* pointerName;
```

## Assignment Syntax

Assignment requires the address-of operator `&` applied to the fully qualified name of the class member. You do not assign it the address of a member from a specific object instance.

```cpp theme={"dark"}
pointerName = &ClassName::memberName;
```

## Dereferencing Syntax

Because the pointer only holds an offset, it cannot be dereferenced on its own. C++ provides two specific pointer-to-member operators to bind the offset to a base object address:

1. **`.*` (Pointer-to-member access operator):** Used when you have a direct object instance or a reference to an object.
2. **`->*` (Pointer-to-member indirect access operator):** Used when you have a standard pointer to an object.

```cpp theme={"dark"}
objectInstance.*pointerName;
objectPointer->*pointerName;
```

## Technical Characteristics

* **Strong Typing:** A pointer to a data member is strictly typed to both the underlying data type *and* the class type. A `int ClassA::*` is fundamentally incompatible with an `int ClassB::*`, even if both classes have identical memory layouts.
* **Non-Static Limitation:** This construct applies exclusively to non-static data members. Static data members exist independently of object instances at fixed memory addresses; therefore, they are pointed to using standard C++ pointers (e.g., `int*`), not pointers to members.
* **Nullability:** Pointers to data members can be initialized or assigned to `nullptr`. Attempting to dereference a null pointer-to-member results in undefined behavior.
* **Polymorphism and Inheritance:** Pointers to data members respect inheritance hierarchies. A pointer to a base class member can be safely applied to a derived class object. However, casting a pointer-to-member from a derived class to a base class requires `static_cast` and is only safe if the base class actually contains that member.

## Complete Mechanics Example

```cpp theme={"dark"}
struct Vector3 {
    float x;
    float y;
    float z;
};

// 1. Declaration: Pointer to a float member within Vector3
float Vector3::* componentPtr;

// 2. Assignment: Pointing to the 'y' member's offset
componentPtr = &Vector3::y;

// Instantiating objects
Vector3 vecA = {1.0f, 2.0f, 3.0f};
Vector3* vecBPtr = new Vector3{4.0f, 5.0f, 6.0f};

// 3. Dereferencing via object instance (.*)
// Resolves to the 'y' member of vecA
float valA = vecA.*componentPtr;       // valA = 2.0f

// 4. Dereferencing via object pointer (->*)
// Resolves to the 'y' member of the object pointed to by vecBPtr
float valB = vecBPtr->*componentPtr;   // valB = 5.0f

// 5. Reassignment
// The pointer can be reassigned to any other member of the exact same type/class signature
componentPtr = &Vector3::z;
float valC = vecA.*componentPtr;       // valC = 3.0f
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
