access specifiers

From cppreference.com
< cpp‎ | language

In a class or struct body define the visibility of following declarators In a inheritance list, define the maximum visibility of inherited members

Contents

[edit] Syntax

public: declarators (1)
protected: declarators (2)
private: declarators (3)
class identifier : public class_name (4)
class identifier : protected class_name (5)
class identifier : private class_name (6)

[edit] Explanation

  1. The symbols declared after the specifier have public accessibility
  2. The symbols declared after the specifier have protected accessibility
  3. The symbols declared after the specifier have private accessibility
  4. The inherited members have the same accessibility as the base class ( either protected or public as private won't be visible in the derived class )
  5. The inherited members have protected accessibility in the derived class
  6. The inherited members have private accessibility in the derived class

[edit] Member accessibility by specifier

public
public members are accessible everywhere, within and outside the class scope
protected
protected members are accessible within the class and its methods and in its descendants
private
private members can be only accessed within the class and its methods

To grant access to external functions or classes to protected or private members, a friendship declaration must be present in the class body

Inherited private members are still present in the class data but cannot be accessed directly

A class has default private accessibility for inheritance and members, a struct has instead a default public accessibility

[edit] Public member access

[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 member functions and friend functions of Base
2) by the member functions and friend functions 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

[edit] Public inheritance

[edit] Protected inheritance

[edit] Private inheritance