[Qt-interest] questions

K. Frank kfrank29.c at gmail.com
Thu Oct 13 16:06:33 CEST 2011


Hello Vincent!

On Thu, Oct 13, 2011 at 2:35 AM, Vincent Cai <wcai at cypress.com> wrote:
> Dear all,
>
>          I am new to C++ and always confused by below questions:
>
> 1.      How to decide a data member to be public or protected or private? I
> know the difference, but sometimes, we can put the data to either one.
>
> 2.      How to decide whether to derive from base class or just define a
> base class instance?

Several other folks have already given you the best advice -- get some
good books, study them, and over time become a more experienced
c++ programmer.

I'd like to add some perspective.  Your questions are all fundamental
design questions.  As such, they don't have any single correct answer.

(I want to build a house.  Should I build a ranch house or a colonial?
Should I use vinyl siding or stucco?  Di I want shake or asphalt shingles
on the roof?  And so on...)

Like all good design questions, the "right" answer depends on the
details of what you are trying to accomplish, and involves all sorts
of trade-offs and compromises.

The object-oriented-design community tends to have a very prescriptive
approach to design:  This is the "right" way; this other way is "wrong."
The questions you ask (public / protected / private and inheritance vs.
composition) are at the heart of object-oriented c++ programming.
You will therefore find a lot of discussion about these issues in the
object-oriented literature, and you will also find a lot of prescriptive,
clear-cut, right / wrong rules about them.

My advice is to understand the reasoning behind these "rules," but not
to apply any such rules blindly.  (I would give this advice to any design
rules people may give you, not just object-oriented design rules.)

The object-oriented reasoning behind the rules advocated by the
object-oriented-design community usually makes sense, but those
reasons may not apply, or may not be of primary importance to your
specific situation.  So, understand the reasoning, see what the benefits
of a specific approach might be, and weigh them against the costs and
disadvantages of that approach in your specific situation.

(One of the frustrations of the object-oriented literature -- and there are
exceptions -- is that they are often silent about the disadvantages and
costs of their recommended approach.)

> I don’t know if you can understand my questions J, but I just hope that
> someone can recommend related book or article for me.

I do understand your questions.  The only problem is that I can't give
you a one-size-fits-all answer to them.  (Nor can anybody else.)

A good place to start (but not stop, of course) is Marshall Cline's
C++ FAQ-Lite:

   http://www.parashift.com/c++-faq-lite/

His sections on inheritance (sections 19 through 25)  have a lot of
material on what you're asking.  (He does pretty much hew to the
object-oriented party line.)

Many of he c++ books on the list that Graham linked to are very good,
and well worth studying.  But many are about c++ in general, and
cover your specific questions only in passing.  Others are specialized
books about specific features and techniques in c++, and don't cover
your specific questions at all.  Having said that, from memory I believe
that  Scott Meyers's "Effective C++" does address your questions, and
is quite good.  (I don't have it in front of me, so maybe someone could
check to make sure I'm not mixing it up with something else.)

The Qt books that Jesus linked to are much more about Qt and don't
really address your specific questions.

Now, to actually try to answer your questions...

Since these are all trade-off, compromise, design questions, we can't
give you an answer without looking at your specific situation -- what
you're trying to accomplish, and what specific design challenges you
face.

If you have a specific question narrowly related to Qt, this list is a very
good place to ask it, and you'll probably get a lot of thoughtful advice.

If it's really just a c++ question (even if Qt is involved, but in a way not
essential to the question), you would probably get better advice from
one of the c++ usenet groups:

   comp.lang.c++
   comp.lang.c++.moderated

(Be forewarned:  The culture on the comp.lang.c++ group, in particular,
can be -- How should I say this? -- a little quirky.  But just work with it,
and you can often get good advice there.)

> Thanks,
>
> Vincent.

Good luck, and Happy Hacking!


K. Frank



More information about the Qt-interest-old mailing list