Everything2
Near Matches
Ignore Exact
Full Text
Everything2

protected

created by moa

(idea) by moa (2.5 mon) (print)   ?   (I like it!) Fri Aug 04 2000 at 0:57:13

In C++, this optional keyword starts declaring the protected data members and/or member functions. The protected members of a class are moderately accessible by other program parts.

It is invoked thus:

class foo
{
public:
//

protected:
protected data members
protected member functions

private:
//
}

The protected section contains data members and member functions that are not accessible to the class instances and functions that do not belong to the class (such as the main() function). Typically, the protected section contains data members and auxiliary member functions (that is, member functions that work behind the secenes to help other member functions, especially the ones declared in the public section).


(thing) by ariels (2.6 d) (print)   ?   (I like it!) Mon Sep 02 2002 at 17:54:54

C++ class member visibility:

In C++, members of the "protected:" section(s) of a class (or struct) definition are visible only in the class and any derived classes. The protected members of a class are often the elements of its implementation.

The following code snippet shows some of the behaviour (it is based on the code in private, to which you should also compare it). Lines with comments starting "*" are compilation errors; since we're discussing visibility, we must show them.

class X {
protected:
  int a;
  int square() {
    return a*a;            // OK (1)
  }

public:
  X(int val) : a(val) {}
};

class Y : public X {
public:
  void print() {
    std::cout << a;        // OK (2)
  }
  void square() {
    a = square();
  }
};

void f()
{
  Y y(17);
  y.print();
  y.square();
  y.print();
  ++ y.a;                 // * NOT OK (3)
}

Notes:

  1. Member functions of X have full access to protected: members.
  2. Y is derived from X, so its member functions also have full access to protected: members of X.
  3. Encouraged by our success, we might try to access Y::a from code outside of X or a derived class of X. This fails, of course.

C++ inheritance visibility:

You may also declare an inheritance relation between two classes "public". It is very similar to public: visibility of members: only member functions of the class and of further derived classes may use that inheritance. But of course, even they are bound by the visibility of the parent class' methods (just like Y was, in the previous example).

This (like private inheritance) is hardly "inheritance" in the sense of an "is-a" relationship, at least not if outside the class and its derived classes. But that's the whole point: anything outside the implementation cannot tell (from external behaviour) that the inheritance exists. Protected inheritance is sometimes used to model an "implemented-by" relationship; as such, it can be useful for some mixins.


printable version
chaos

private public Is being domesticated an evolutionary advantage? Duff
Abisko C++ class space
Sport climbing Class 'A' Reserve January 19, 2003 Child Support Agency
Endangered species Burdekin Duck The Everything People Registry : Greece MPEG Audio Frame Header
prototype June 10, 2002 protect set
protection const Truth Sodium azide
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Little presents from the Node Fairy:
The Russian Revolution: Fictional diary entries from a loyalist factory worker
They hate us because we're wonderful
Universal Declaration of Human Rights
Over-educated Supervillains
Human Genome Project
reality-based community
emits showers of sparks
Our eyes reflect the light of dead suns
Finnish proverbs
Moon River
L'Académie française
mouser
London Bridge
New Writeups
antigravpussy
One fly amonst many(person)
sam512
Moon Base Shackleton, 1978(fiction)
Pavlovna
toy boy(person)
XWiz
tear jerker(review)
Heitah
Anarchy is Order(idea)
jessicaj
July 26, 2008(dream)
Berek
ABBA(person)
devolution
k-hole(place)
Nadine_2
The Sound Of Madness(review)
Twin Eclipse
Conversations with God: An Uncommon Dialogue(idea)
SwimmingMonkey
Conversations with Fo Fo, the Loneliest dog in Purgatory(fiction)
locke baron
lynx(thing)
Simulacron3
Reality, Dimensions and the Natural Ontology(essay)
SubSane
Making Love to a 9-Foot Woman(person)
Ouzo
Thoughts(idea)
This affordable entertainment brought to you by The Everything Development Company