Re: Use the accessor! Was: Copy Constructor Usage

From: Ron House (house_at_usq.edu.au)
Date: 02/09/05


Date: Wed, 09 Feb 2005 14:48:49 +1000

Francis Glassborow wrote:

> Accessor functions should be designed to deal with attributes of a type.
> Whether those attributes map directly to data members is an
> implementation decision.

> However copy construction is intended to copy the data (sometimes an
> item of data is not copied by design. For example you might want to
> allow copies of originals and disallow copies of copies, in which case
> one data member will be an is_a_copy flag which is checked by the copy
> ctor, if it is set an exception is thrown if it isn't it gets set in the
> copy.)

I think you are getting too hung up on the copy constructor bit. The
original poster made a general comment, highlighting efficiency, which
just happened to use code in the copy constructor as the instance at
hand. My point was less specific: even inside a class, rely on its
internals as little as possible. Most definitely do _not_ ignore the
accessor fns on some bogus efficiency argument. But even in the copy
constructor, I think this point has force. Of course, some fields are
tightly tied to the specific implementation, and the fact that an
accessor happens to return that field is not a good enough reason to use
it. For example, a field 'leng' might map onto an accessor 'size()'; but
leng exists only as part of a very specific implementation. In another,
it might involve searching for a zero byte. On the other hand, a field
like 'id' maps onto an accessor 'client-id()' for a quite different
reason. In this case client-id is part of the data content of the
object, not a report on a property. So it would make more sense to use
accessors for id than for leng.

But all this depends on thoughtful analysis of the actual class at hand;
the OP just told us we didn't have to do it and it would be more
efficient not to. I think that's carelessness, and carelessness that
leans in favour of making more errors, not fewer. But doing this kind of
analysis will reveal many small glitches and sloppinesses in our
designs; taking a pass on the question leaves our code less than ideal.
That is the kind of thinking to which I was moved to post a
counterpoint. Of course, for a quick and dirty class, that might also be
just fine; but at least ask the questions and don't unthinkingly fall
back on the beguiling but bad reason, small savings of efficiency.

> There is no reason to suppose that accessors will correctly cover the
> data that needs copying (they do in this case, but we should learn
> general principles rather than special cases)

And the general principle is what? Always copy all the data? I have the
default copy constructor for that.

> Note that any time you change the data storage for a class you will
> necessarily have to revisit all the constructors and at least consider
> the other special functions.

Absolutely. No style or design rules give us a free lunch.

-- 
Ron House     house@usq.edu.au
               http://www.sci.usq.edu.au/staff/house


Relevant Pages

  • Re: Class in another file
    ... > oriented design is that it is a technique that focuses design on ... programmer did was: design a way of using a structure of standard ... different but related object (i.e. what's now called a "constructor ... that's not as nice as true parameter overloading that Common Lisp ...
    (comp.lang.java.programmer)
  • Re: C++ design question
    ... > Why not invoke the barDerived constructor? ... which addresses design issues rather ... barDerived1, fooDerived2 with corresponding barDerived2, fooDerivedN ... a burden on developers of the various fooDerivedN. ...
    (comp.object)
  • Re: A new, simpler GPS?
    ... problems with the GPS12 or 60CS manuals. ... complained in the past that "map and compass" is not a single ... You can take a compass out too. ... case of good design. ...
    (uk.rec.walking)
  • Re: We Interrupt This Submission . . .
    ... this attracts the attention of both men ... attract attention, and the purpose of a cover is to get people to pick ... You'd probably respond a lot more than you would to a map, ... My daughter showed me the web-site of a design college and I had to ...
    (rec.arts.sf.composition)
  • Re: "Program to an interface" - When to break a design pattern
    ... Now the design principle says, the method signature should instead be ... ordering of LinkedHashMap - Map does not have that. ... If what's returned actually is a Map, ...
    (comp.lang.java.programmer)