Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

protected — protected access

The protected keyword controls member access or inheritance access. It makes members available to derived classes without exposing them to ordinary outside code, but adds restrictions on the object expression used to access non-static members.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

class Base {
protected:
    int value = 0;
};

class Derived : protected Base {};

protected: sets access for subsequent member declarations until the next access specifier or the end of the class. Access is available to members and friends of the declaring class and, subject to inheritance accessibility, members and friends of derived classes. private does not grant this access to derived classes; public imposes no access restriction. Disallowed access is a compile-time error, not a runtime check.

Protected non-static data members and member functions have an additional rule: when access relies on the privileges of a derived class Derived, the object expression's type must be Derived or a class derived from it. Thus other.value works for const Derived&, but not for const Base&, even if the actual object is a Derived. This additional check does not apply to static members. When forming a pointer-to-member for a protected member, the qualifier must name the class whose privileges grant access or a class derived from it. Thus inside Derived, use &Derived::value, not &Base::value. By contrast, if s is a protected static data member of Base, a member of Derived can use &Base::s to form an ordinary pointer.

In class Derived : protected Base, the keyword controls inheritance: public and protected members of Base are accessible through Derived as protected. This does not grant access to private members of Base. Ordinary outside code then cannot implicitly convert Derived* to Base*; members and friends of Derived can. This is a different use from the protected: label inside a class.

The example uses public inheritance to isolate protected member access. read() exposes the value through a public function, while the two commented statements illustrate forbidden access. This entry targets C++23; the supplied standard URL hosts an evolving working draft, not a frozen C++23 edition. Using a compiler flag does not attest operation in a production runtime.

Examples

#include <iostream>

class Base {
protected:
    int value = 7;
};

class Derived : public Base {
public:
    int read() const { return value; }

    void copyFrom(const Derived& other) {
        value = other.value;
    }

    // void copyFromBase(const Base& other) { value = other.value; }
};

int main() {
    Derived first;
    Derived second;
    second.copyFrom(first);
    std::cout << second.read() << '\n';
    // std::cout << second.value << '\n';
}

Result

7

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources