Re: Use the accessor! Was: Copy Constructor Usage
From: Ron House (house_at_usq.edu.au)
Date: 02/09/05
- Next message: Jerry Coffin: "Re: Constructors: Help me answer students question"
- Previous message: Ron House: "Re: Use the accessor! Was: Copy Constructor Usage"
- In reply to: Francis Glassborow: "Re: Use the accessor! Was: Copy Constructor Usage"
- Next in thread: Francis Glassborow: "Re: Use the accessor! Was: Copy Constructor Usage"
- Reply: Francis Glassborow: "Re: Use the accessor! Was: Copy Constructor Usage"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Jerry Coffin: "Re: Constructors: Help me answer students question"
- Previous message: Ron House: "Re: Use the accessor! Was: Copy Constructor Usage"
- In reply to: Francis Glassborow: "Re: Use the accessor! Was: Copy Constructor Usage"
- Next in thread: Francis Glassborow: "Re: Use the accessor! Was: Copy Constructor Usage"
- Reply: Francis Glassborow: "Re: Use the accessor! Was: Copy Constructor Usage"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|