1

What kind of data or function is better to be put in the protected area of C++ class? And what's the benefit of doing it?

injoy
  • 3,993
  • 10
  • 40
  • 69
  • Maybe you can take a look [here](http://stackoverflow.com/questions/224966/private-and-protected-members-c). – pzaenger Sep 17 '13 at 14:45
  • This is one of the few areas covered in *any* C++ instructional material, no matter how well or poorly written. – WhozCraig Sep 17 '13 at 14:47
  • @WhozCraig: the benefit part yes, but whether to put it or not ? Nay... most tutorials screw it up. – Matthieu M. Sep 17 '13 at 15:12

3 Answers3

2

What kind of data or function is better to be put in the protected area of C++ class?

Items which you want to be accessible through inheritance.

And what's the benefit of doing it?

The protected keyword specifies that the public and protected members of the base class are protected members of its derived classes.

Protected members are not as private as private members, which are accessible only to members of the class in which they are declared, but they are not as public as public members, which are accessible in any function. Protected members that are also declared as static are accessible to any friend or member function of a derived class.

Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.

Check MSDN

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

The protected class members can be accessed by any classes derived from that class and friend classes.

AngelCastillo
  • 2,385
  • 2
  • 18
  • 26
0

protected is nearly equivalent to public: any derived class might use, so the set of accessors is unbounded; a derived class might even make a public wrapper that directly call the base protected method if necessary.

As a result, my advice is simple:

  • Data should be either public or private
  • Methods should be either public or private, in general

The only exception for methods is that constructors, assignment operators and destructors can be made public to prevent the construction/copy/assignment/destruction of a base class without precluding its children to be constructed/copied/assigned to/destructed.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • That is a bold statement. IMO `protected` is as close to `public` as `private`. Also why do you want to make a wrapper class, when you want to abuse system you can go `#define private public` or `-Dprivate=public -Dprotected=public -Dclass=struct`. There is much use to `protected` e.g. when designing interfaces you often write a public `virtual` caller, that in basic implementation "consists" of several `protected` stages. Then descendant classes are given much of freedom in their design, which is nice. – luk32 Sep 17 '13 at 15:50
  • @luk32: it is bold, but it is necessary. Too many people fail to understand that you cannot rely on derived classes to maintain a class invariants; and `protected` is often used to open such a hole. I don't deny using `protected` cannot be convenient, I argue that using it however is bad design. Proper solutions might be slightly more complicated, though in practice it has never happened to me. – Matthieu M. Sep 17 '13 at 18:01