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.

A public member variable in C++ is a class or struct data member declared under the public access specifier, granting unrestricted accessibility from any scope where the class or instantiated object is visible. While it exposes the internal state directly to external code, the mutability of this state depends on both the variable’s own const qualification and the const qualification of the object instance through which it is accessed (unless the member is explicitly declared mutable). For example, a standard non-const public member accessed through a const object instance becomes strictly read-only. In C++, members of a class are private by default, requiring an explicit public: label to change their accessibility. Conversely, members of a struct are public by default. It is important to note that access specifiers control accessibility, not visibility. A private member remains fully visible to name lookup from outside the class (allowing it to participate in overload resolution), but will trigger a compilation error due to an access violation if selected by an unauthorized scope.

Syntax and Declaration

Public member variables are declared within the class definition following the public: access label. A member is fundamentally either static or non-static, and can independently be const or non-const.
class Entity {
public:
    int identifier;                // Non-static, non-const public member
    const float maxSpeed = 100.0f; // Non-static, const public member
    static int globalCount;        // Static, non-const public member
    static const int maxEntities;  // Static, const public member
};

// Static member definitions (typically in a single translation unit)
int Entity::globalCount = 0;
const int Entity::maxEntities = 1000;

struct Vector3 {
    // Public by default in a struct
    float x, y, z; 
};

Access Mechanics

Accessing a public member variable depends on whether the member is static or non-static, and how the object is referenced in memory. 1. Instance Access (Dot and Arrow Operators) Non-static public members are bound to specific object instances. When working with a direct instance or a reference, the member access operator (.) is used. When working with a pointer to an object, the member access through pointer operator (->) is used.
Entity entityInstance;
entityInstance.identifier = 42;       // Dot operator

Entity* entityPtr = &entityInstance;
entityPtr->identifier = 256;          // Arrow operator

// Value-initialized to satisfy const object rules for types 
// without a user-provided default constructor
const Entity readOnlyEntity{};        
// readOnlyEntity.identifier = 10;    // Error: identifier is read-only via const object
2. Static Access (Scope Resolution Operator) Static public members belong to the class type itself rather than any instantiated object. They are accessed directly using the class name and the scope resolution operator (::).
Entity::globalCount = 1; // Accessed without an object instance
3. Pointer-to-Member Access C++ allows creating a pointer to a class data member. Access control is enforced when the pointer-to-member is formed (when the member’s name is used), not when it is dereferenced. Forming a pointer-to-member from an external, unprivileged scope requires the member to be public. Once a valid pointer-to-member is formed, it can be dereferenced using the .* (object) or ->* (pointer) operators.
// Form a pointer to a public non-static int member of Entity.
// Access control is checked at this exact point.
int Entity::* memberPtr = &Entity::identifier;

Entity obj;
// Dereference the pointer-to-member
obj.*memberPtr = 50; // Syntactically accesses obj.identifier

Technical Characteristics

  • Memory Layout: Non-static public member variables contribute directly to the memory footprint of an object instance (sizeof(ClassName)). The compiler allocates memory for these variables in the order they are declared within the class, subject to standard alignment and padding rules. Conversely, static public members are stored in a separate, global memory location and do not contribute to the sizeof an instance.
  • Initialization Order: Regardless of their access specifier, non-static member variables are always initialized in the exact order they are declared in the class definition, not the order they appear in a constructor’s initializer list.
  • Inheritance Behavior: The accessibility of a public member variable in a derived class depends on the type of inheritance used:
    • public inheritance: The variable remains public in the derived class.
    • protected inheritance: The variable becomes protected in the derived class.
    • private inheritance: The variable becomes private in the derived class.
  • Standard Layout and Triviality: The C++ standard dictates that for a class to qualify as a Standard Layout Type, all non-static data members must share the same access control. Therefore, a class where all non-static data members are public satisfies this specific requirement (assuming other criteria, like lacking virtual functions, are met), guaranteeing a predictable, C-compatible memory layout. However, safely copying such an object using low-level memory operations like std::memcpy is governed by a distinct property: the class must be Trivially Copyable. A class can have exclusively public members and be Standard Layout, but if it possesses a user-defined destructor or non-trivial copy constructor, it is not Trivially Copyable, making std::memcpy unsafe and a source of undefined behavior.
Master C++ with Deep Grasping Methodology!Learn More