Topic: Protected data members


Author: u865151@student.canberra.edu.au (Burger / John Adriaan (ISE))
Date: Fri, 17 Feb 95 10:34:56 GMT
Raw View
In article <3hu1g0$pea@engnews2.Eng.Sun.COM>,
Steve Clamage <clamage@Eng.Sun.COM> wrote:
>As a separate but related issue, many C++ experts now recommend against
>using protected members at all. The reasons for making something private
>are to hide implementation details that could be misused, and to allow a
>change in implementation details without affecting code in other modules.
>
>Making something public is a promise that the interface will be preserved.
>
>Making something protected means that you don't want to expose it to
>just anyone. Yet you cannot predict what classes will be derived from
>your class, or by whom, or under what circumstances. (Notice the contrast
>with friend functions: there are no friend functions except the ones
>you declare and write.) This means you cannot change the protected interface
>either. Further, you can no longer ensure that the protected interface will
>not be misused by the author of a derived class who did not understand the
>consistency requirements. You might as well make the thing public.

I've heard (some of) the reservations about protected data members. I use
them in one particular case: rather than get into details I'll generalise
to a simple hypothetical example.

    MS Windows programming uses a handle to a window of type HWND, and this
gets passed to Windows fuctions. If I wanted to encapsulate a window (as many
people have), "HWND handle;" would appear in the base class. Making this
public is 'bad', since it's not for public consumption - the class should
protect the variable from the user. Making it private is 'bad', because
derived classes may want to access/modify it (and the idea of making
interface functions that allows these is silly, since you might as well leave
the variable accessible).

   Making it protected, however, seems to solve both problems. I agree
with the statement about "cannot change the protected interface", but my
point is I didn't define it. Microsoft better not change it!

   Is this a valid argument for protected data members?

   John Burger