> ## 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++ Const Member Variable

A `const` member variable is a non-static data member of a class declared with the `const` type qualifier, dictating that its value cannot be modified after the object's initialization phase completes. While modifying a `const` member after initialization results in undefined behavior, the member itself typically resides in standard read-write memory (such as the stack or heap) alongside the rest of the object instance, unless the entire object is declared `const` and allocated in static storage.

Because assignment is prohibited after initialization, `const` member variables cannot be assigned values within the body of a constructor. They must be initialized during the object creation phase using one of three mechanisms:

1. **Member Initializer List:** The standard method for initializing `const` members, executed before the constructor body.
2. **In-class Brace-or-Equal Initialization:** Available from C++11 onwards, allowing a default value to be specified directly at the point of declaration.
3. **Aggregate Initialization:** For aggregate types (e.g., structs without user-declared constructors), `const` members can be initialized directly via brace initialization at the point of object instantiation.

```cpp theme={"dark"}
// 3. Aggregate Initialization
struct Config {
    const int retries; 
};

class Connection {
private:
    const int max_retries;       // Must be initialized via initializer list
    const double timeout = 5.5;  // 2. C++11 in-class initialization

public:
    // 1. Initialization via member initializer list
    Connection(int retries) : max_retries(retries) {
        // max_retries = retries; // COMPILER ERROR: Assignment of read-only member
    }
    
    // Overriding the in-class initializer
    Connection(int retries, double custom_timeout) 
        : max_retries(retries), timeout(custom_timeout) {} 
};

// Instantiating the aggregate type
Config c{5}; 
```

## Compiler Implications

The inclusion of a non-static `const` member variable fundamentally alters how the compiler generates special member functions for the class:

* **Implicit Deletion of Assignment Operators:** The compiler will implicitly delete the default copy assignment operator (`operator=`) and move assignment operator. Because a `const` member cannot be reassigned, the compiler cannot safely generate a function to overwrite an existing object's state.
* **Default Constructor Requirements:** If a `const` member lacks an in-class initializer, the compiler's generation of the default constructor depends on the member's type:
  * If the `const` member is a fundamental type (e.g., `int`), a pointer, or a class type without a *user-provided* default constructor (and is not otherwise const-default-constructible), the compiler will implicitly delete the enclosing class's default constructor.
  * If the `const` member is a class type with a *user-provided* default constructor (e.g., `const std::string`), the compiler will successfully generate the enclosing class's default constructor and implicitly invoke the member's default constructor.

```cpp theme={"dark"}
#include <string>

struct Point {
    int x; // Implicitly generated default constructor (not user-provided)
};

struct InvalidNode {
    const Point origin; // Lacks in-class initializer and user-provided default constructor
    // The compiler implicitly defines InvalidNode() as deleted.
};

class Node {
public:
    const Point origin;         
    const std::string label;    // Class type with user-provided default constructor

    // Correct: Explicitly initializing the const member via initializer list
    Node() : origin{0} {}       // 'label' is implicitly initialized to ""

    Node(Point p, std::string l) : origin(p), label(l) {}
};

void instantiate_nodes() {
    // InvalidNode bad_node; // COMPILER ERROR: use of deleted function 'InvalidNode::InvalidNode()'

    Node nodeA({10}, "Alpha");
    Node nodeB({20}, "Beta");

    // nodeA = nodeB; // COMPILER ERROR: copy assignment operator is implicitly deleted
}
```

## Instance-Level vs. Class-Level Immutability

A standard `const` member variable is non-static, meaning it is allocated per object instance. While its value is immutable for the lifetime of that specific object, different instances of the class can hold entirely different values for that `const` member (determined at construction).

To create a compile-time constant shared across all instances of the class, the member must be declared as `static const` (or `static constexpr`), which removes it from the object's memory layout and places it in the program's static data segment.

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