Like Tree1Likes

Why use private?

This is a discussion on Why use private? within the C++ Programming forums, part of the General Programming Boards category; Protected should definitely be the exception not the rule. Always prefer private over protected. A lot of protected methods usually ...

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Protected should definitely be the exception not the rule. Always prefer private over protected. A lot of protected methods usually means your inheritance hierarchy likely needs some tuning. In all of the classes I have written in the last ten years I have probably used protected in about 1% of them. Private in a base class communicates that the designer is saying the derived class should not need access to the method or variable and if they do...then there is a problem with the design of the derived class. If the client has access to the code they would have to change the private to protected to gain access to it. This alone would trigger a red flag in their head that perhaps they should not be mucking around with the original base classes.

    In some instances protected methods are needed such as when the base class is providing some core functionality that it only wants to expose to derived classes. But since I normally derive from interfaces (read pure virtual or abstract base classes) and I rarely, if ever, derive from a concrete class I almost never have a need to use protected. Also this core functionality could just as easily be exposed via a design pattern like flyweight. I would agree with CornedBee in that there should never be any protected member variables. The only time I would ever use any public member variables is if profiling indicated that access to variables was significantly impacted. Even then I would vomit in the back of my throat if I made any variables public. As well if I worked in a studio with more than a few developers touching the same code I would prepare myself for the eventual firestorm that would be headed my way due to my class that had public variables. I have debugged code that did this and wow what a mess. This class poking around inside that class which pokes around inside another class which alters the value which another class uses and then pokes around in another class....you can barely find out who or what is changing the variable that is causing the code to crash. A huge nightmare. I will step out on a limb here and say that if you make member variables public in an existing code line for any reason at all you will create a firestorm later that could put the code line in a potentially unusable or unmaintainable state.
    Last edited by VirtualAce; 03-08-2012 at 08:23 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 03:20 AM
  2. private/public get/set
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-04-2008, 01:49 AM
  3. The Private game 2
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-04-2002, 03:20 AM
  4. Why Private
    By Manitoadlet in forum C++ Programming
    Replies: 8
    Last Post: 09-16-2002, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21