access specifiers

From cppreference.com
< cpp‎ | language

In a member-specification of a class/struct or union, define the visibility of subsequent members.

In a base-clause of a derived class declaration, define the accessibility of inherited members of the subsequent base-specifiers.

Contents

[edit] Syntax

public : member-declarations (1)
protected : member-declarations (2)
private : member-declarations (3)
class_name : public base_classes (4)
class_name : protected base_classes (5)
class_name : private base_classes (6)
1) The members declared after the specifier have public member access
2) The members declared declared after the specifier have protected member access
3) The members declared after the specifier have private member access
4) Public inheritance: the members of the base classes listed after the specifier keep their member access in the derived class
5) Protected inheritance: the public of the base classes listed after the specifier are protected members of the derived class
6) Private inheritance: the public and protected members of the base classes listed after the specifier are private members of the derived class

[edit] Explanation

The name of every class member (static, non-static, function, type, etc) has an associated "member access". When a name of the member is used anywhere a program, its access is checked, and if it does not satisfy the access rules, the program does not compile:

#include <iostream>
class Example {
 public: // all declarations after this point are public
    void add(int x) { // member "add" has public access
        n += x; // OK: private Example::n can be accessed from Example::add
    }
 private: // all declarations after this point are private
    int n = 0; // member "n" has private access
};
int main()
{
    Example e;
    e.add(1); // OK: public Example::add can be accessed from main
//  e.n = 7;  // Error: private Example::n cannot be accessed from main
}


Access specifiers give the author of the class the ability to decide which class members are accessible to the users of the class (that is, the interface) and which members are for internal use of the class (the implementation)

[edit] In detail

All members of a class (bodies of member functions, initializers of member objects, and the entire nested class definitions) have access to all the names to which a class can access. A local class within a member function has access to all the names the member function itself can access.

A class defined with the keyword class has private access for its members and its base classes by default. A class defined with the keyword struct has public access for its members and its base classes by default. A union has public access for its members by default.

To grant access to additional functions or classes to protected or private members, a friendship declaration may be used.

Accessibility applies to all names with no regard to their origin, so a name introduced by a typedef or using declarations is checked, not the name it refers to.

class A : X {
  class B { }; // B is private in A
public:
  typedef B BB; // BB is public
};
void f() {
  A::B y; // error, A::B is private
  A::BB x; // OK, A::BB is public
}

Member access does not affect visibility: names of private and privately-inherited members are visible and considered by overload resolution, implicit conversions to inaccessible base classes are still considered, etc. Member access check is the last step after any given language construct is interpreted. The intent of this rule is that replacing any private with public never alters the behavior of the program.

Access checking for the names used in default function arguments as well as in the default template parameters is performed at the point of declaration, not at the point of use.

Access rules for the names of virtual functions are checked at the call point using the type of the expression used to denote the object for which the member function is called. The access of the final overrider is ignored.

struct B { virtual int f(); }; // f is public in B
class D : public B { private: int f(); }; // f is private in D
void f() {
 D d;
 B& b = d;
 b.f(); // OK: B::f() is public, D::f() is invoked even though it's private
 d.f(); // error: D::f() is private
}

A name that is private according to unqualified name lookup, may be accessible through qualified name lookup:

class A { };
class B : private A { };
class C : public B {
   A* p; // error: unqualified name lookup finds A as the private base of B
   ::A* q; // OK, qualified name lookup finds the namespace-level declaration
};

A name that is accessible through multiple paths in the inheritance graph has the access of the path with the most access:

class W { public: void f(); };
class A : private virtual W { };
class B : public virtual W { };
class C : public A, public B {
void f() { W::f(); } // OK, W is accessible to C through B
};

Any number of access specifiers may appear within a class, in any order. Member access specifiers may affect class layout: the addresses of non-static data members#Layout are only guaranteed to increase in order of declaration for the members with the same access. For StandardLayoutType, all non-static data members must have the same access.

When a member is redeclared within the same class, it must do so under the same member access:

struct S {
  class A; // S::A is public
private:
  class A {}; // error: cannot change access
};

[edit] Public member access

Public members form a part of the public interface of the class (other parts of the public interface are the non-member functions found by ADL).

A public member of a class is accessible everywhere.

class S {
 public: // n, f, E, A, B, C, U are public members
    int n;
    static void f() {}
    enum E {A, B, C};
    struct U {};
};
int main()
{
    S::f(); // S::f is accessible in main
    S s;
    s.n = S::B; // S::n and S::B are accesisble in main
    S::U x; // S::U is accessible in main
}

[edit] Protected member access

Protected members form the interface for the derived classes (which is distinct from the public interface of the class).

A protected member of a class Base can only be accessed

1) by the members and friends of Base
2) by the members and friends of any class derived from Base, but only when operating on an object of a type that is derived from Base (including this)
struct Base {
 protected:
    int i;
 private:
    void g(Base& b, struct Derived& d);
};
 
struct Derived : Base {
    void f(Base& b, Derived& d) // member function of a derived class
    {
        ++d.i; // okay: the type of d is Derived
        ++i; // okay: the type of the implied '*this' is Derived
//      ++b.i; // error: can't access a protected member through Base
    }
};
 
void Base::g(Base& b, Derived& d) // member function of Base
{
    ++i; // okay
    ++b.i; // okay
    ++d.i; // okay
}
 
void x(Base& b, Derived& d) // non-member non-friend
{
//    ++b.i; // error: no access from non-member
//    ++d.i; // error: no access from non-member
}

When a pointer to a protected member is formed, it must use a derived class in its declaration:

struct Base {
 protected:
    int i;
};
 
struct Derived : Base {
    void f()
    {
//      int Base::* ptr = &Base::i;    // error: must name using Derived
        int Base::* ptr = &Derived::i; // okay
    }
};

[edit] Private member access

Private members form the implementation of a class, as well as the private interface for the other members of the class.

A private member of a class can only be accessed by the members and friends of that class, regardless of whether the members are on the same or different instances:

class S {
 private:
    int n; // S::n is private
 public:
    S() : n(10) {} // this->n is accessible in S::S
    S(const S& other) : n(other.n) {} // other.n is accessible in S::S
};

The explicit cast (C-style and function-style) allows casting from a derived lvalue to reference to its private base, or from a pointer to derived to a pointer to its private base.

[edit] Inheritance

See derived classes for the meaning of public, protected, and private inheritance.