A
Act
Why is it suggested to not define data members as "protected"?
Thanks for help!
Thanks for help!
Why is it suggested to not define data members as "protected"?
Thanks for help!
Act said:Why is it suggested to not define data members as "protected"?
Act said:Why is it suggested to not define data members as "protected"?
Act said:Why is it suggested to not define data members as "protected"?
Act said:Why is it suggested to not define data members as "protected"?
IMO that's too general an advice to be meaningful. From
where did you get this suggestion?
David Hilsee said:Some suggest that, some don't. I generally avoid protected data members,
but some people don't mind. For instance, the answers in the FAQ
(http://www.parashift.com/c++-faq-lite/) under "Inheritance - basics",
questions 8 and 9 lean towards protected data.
I must admit that the
paragraph deriding "purists" is a bit, er, melodramatic.
"Lean"? hehe
My take on this is that whenever you use the keyword "protected" you are
expecting your client to derive from the class. In such a case I don't see
why protected data is any better than public data -- either way you are
giving your client full access to your implementation details.
The FAQ makes
a big distinction between cases where you expect the client to be "on your
team" or not.
Stroustrup for one (referring to protected data in particular). See section
15.3.1.1 in http://www.research.att.com/~bs/3rd.html
A good point, but "on your team", if that's the expression the FAQ uses, is
a bit misleading.
It should be more like "part of your body"... ;-)
But these times even that is perhaps not strong enough: a derived class is
a very, Very, VERY intimate & strong relationship.
I'm not sure what you mean by "part of your body". The FAQ was basically
arguing that derived classes tend to be limited in number and maintained by
your team, so a base class change that causes the derived classes to change
will not cause much grief.
A derived class's relationship with its base is only as intimate as the base
allows it to be.
For example, a class that derives from std::vector has no stronger
relationship than any other client.
If the base exposes a lot in
its "protected interface", then yes, the derived class may have to be
updated if the base class changes.
However, this is not required by any means.
Alf P. Steinbach said:* John Brown:
Perhaps you would kindly quote the passage instead of giving a reference
that doesn't contain or give further reference to the passage.
Alf P. Steinbach said:* David Hilsee:
True but irrelevant: when you design for inheritance you're aiming for the
very Very VERY intimate relationship, not the armored warfare relationship;
and there's basically nothing in-between the these points, for any brink in
the armor gives the first kind automatically.
I don't care to check that for accuracy, but again: even if true it's
irrelevant. You might want to check out std::list for a counter-example.
Replace "a lot" with "anything", and tack onto the end, "in the protected
interface"... ;-)
In other words, that statement is misleading in two ways.
A more general & correct statement: if a base class is changed, then any
derived class may have to be updated. And the consequence is that the
cost of design, the cost of updates, the cost of deriving and using, the
cost of (in)efficiency, and so on, must be balanced. That's an engineering
decision requiring sound judgement: it should not, IMO, be made using
cookbook recipe rules like "don't use protected for data members".
Well, _you_ may aim for a very intimate relationship. I was pointing out
that an intimate relationship can be avoided. IMHO, the usage of an extra
exposed detail or two in the protected interface does not automatically
constitute a "very very VERY intimate relationship". It's more intimate
than others who are restricted to the public interface, that's for sure.
I'd argue that there is certainly an area in between "armored warfare" and
"extremely intimate".
How is std::list a counterexample? It has no standard protected members.
members.
Bugger this keyboard. It doesn't write what I mean. Three examples of
standard containers with protected data members are std::queue, std::stack
and std:riority_queue (this time I checked it out, hope this helps).
A derived class (on the one hand) and so-called client code (on the other
hand) are two different kinds of client.
A derived class is a fully trusted client: you're giving it access to your
innards, and if it screws up then you (or it) is dead anyway.
But these times even that is perhaps not strong enough: a derived class is
a very, Very, VERY intimate & strong relationship.
... But on the gripping hand ...
Cy Edmunds said:The FAQ also mentions the dubious practice of replacing protected data with
protected get/set methods. I agree, but I don't think public get/set methods
are so hot either.
Howard said:You agree with a "dubious practice"? That's a strange thing to say... (Or
did you mean you agree that it's a dubious practice? If so, why?)
In any case, you either have to have get/set methods, or you have to expose
the data itself, right? I'm confused which it is that you prefer...?
For me, I tend to use public data for POD structs (e.g., something simple
like a three-d vector with .x, .y and .z members), but I prefer private data
for my more complex classes. However, I do use protected data in some
instances, because I'm generally writing all the classes involved, base and
derived, and it's simply quicker to leave the data as protected and directly
access the members instead of writing a bunch of getters/setters. (There
are also performance issues involved in a few cases, but most of those
involve me deriving from a third-party class that's already made the design
decisions for me.)
As for the visibility of the get/set methods, whether they're protected or
public really depends upon whether I want external code to read or write to
those (perhaps with some kind of checks/asserts/side-effects, etc.), or if
only the derived classes need to read and/or modify those values. If only
derived classes need to know about these "details", the protected
gettters/setters make sense, right? But if they're required information for
the world in general (like the Name of a Person), then surely public
getters/setters are *required*! I mean, if the data itself isn't public,
then what other method of access is there?
case.In summary, my take on this is that, well, it all depends! No hard-and-fast
rule is likely to apply to all the needs of all the programmers all the
time. We're paid (well, in general), to make intelligent decisions, and we
ought to be able to justify a given decision by thinking it through in the
first place. So if your boss asks "why is this data private instead of
protected", you shouldn't be answering "because protected data is bad", but
rather you should explain why you made the decision in that particular
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.