> ## 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++ Function Pointer

A function pointer is a variable that stores the memory address of executable code (a function) rather than a data value. It allows a program to invoke a function indirectly by dereferencing the pointer, provided the pointer's type signature strictly matches the target function's return type and parameter list.

## Syntax and Declaration

The declaration of a function pointer requires specifying the exact signature of the functions it can point to. The asterisk `*` and the pointer name must be enclosed in parentheses to override standard operator precedence; otherwise, the compiler interprets it as a function returning a pointer.

```cpp theme={"dark"}
// return_type (*pointer_name)(parameter_type_1, parameter_type_2, ...);

int (*operationPtr)(int, int); 
```

## Initialization and Assignment

A function pointer is initialized by assigning it the uninvoked name of a function. The address-of operator (`&`) is optional because a function name implicitly decays into a pointer to that function.

```cpp theme={"dark"}
int add(int a, int b) {
    return a + b;
}

// Both initializations are strictly equivalent
int (*ptr1)(int, int) = add;
int (*ptr2)(int, int) = &add; 
```

## Invocation

Invoking a function through a pointer can be done using implicit or explicit dereferencing. Both approaches yield identical machine code.

```cpp theme={"dark"}
// Implicit dereference (Standard C++ style)
int result1 = ptr1(5, 3);

// Explicit dereference (Legacy C style)
int result2 = (*ptr1)(5, 3);
```

## Type Aliasing

Because raw function pointer syntax can become difficult to parse—especially when used as return types or parameters—C++ provides `typedef` and the modern `using` keyword to create type aliases.

```cpp theme={"dark"}
// Legacy typedef approach
typedef int (*OperationFunc)(int, int);

// Modern C++ type alias approach (Recommended)
using OperationFuncAlias = int (*)(int, int);

// Usage
OperationFuncAlias myPtr = add;
```

## Pointers to Member Functions

In C++, standard function pointers cannot store the address of non-static member functions. This is because non-static member functions require an implicit `this` pointer to operate on an object instance.

To point to a non-static member function, you must use the pointer-to-member syntax (`Class::*`).

```cpp theme={"dark"}
class Calculator {
public:
    int multiply(int a, int b) { return a * b; }
};

// Declaration of a pointer to a member function of Calculator
int (Calculator::*memPtr)(int, int) = &Calculator::multiply;
```

Invoking a pointer to a member function requires an instantiated object of the class and the use of the pointer-to-member operators (`.*` for objects/references, or `->*` for pointers).

```cpp theme={"dark"}
Calculator calc;
Calculator* calcPtr = &calc;

// Invocation via object instance
int res1 = (calc.*memPtr)(4, 5);

// Invocation via object pointer
int res2 = (calcPtr->*memPtr)(4, 5);
```

*Note: Static member functions do not have an implicit `this` pointer. Therefore, they decay into standard function pointers and do not require the pointer-to-member syntax.*

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