Everything2
Near Matches
Ignore Exact
Full Text
Everything2

protected

created by moa

(idea) by moa (2.3 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 (14.9 hr) (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
Look at this mess the Death Borg made!
Lipogram
The oppression of definition
How to DJ
It is easier for a camel to go through the eye of a needle
Numbers Station
split infinitive
Andre the Giant
Red Headed Stranger
I had a brother, once
knots and ribbons at the ends of her arms
Chronology of the Crusades
Fifteen Elvish ways to die
America
New Writeups
Cuckowski
Slavonic Princess(poetry)
Heitah
Posthumous Oscar(thing)
ignis_glaciesque
University of South Florida(place)
ignis_glaciesque
Flogstaskriket(idea)
liveforever
Caesar's last breath(idea)
dagnyswaggart
she wants to believe(personal)
antigravpussy
he doesn't know, but her eyes widen too far(thing)
dagnyswaggart
Wild tides guard her secrets(poetry)
Lord Brawl
Caesar's last breath(poetry)
locke baron
Forgotten things in space(fiction)
sitaraika
Colours(idea)
etouffee
Wild tides guard her secrets(poetry)
Lord Brawl
Dr. Horrible's Sing-Along Blog(review)
a8ksh4
regret(idea)
Heisenberg
Editor Log: July 2008(log)
Everything 2 is brought to you by the letter C and The Everything Development Company