Re: Public member variables acceptable?
alan_mckenney1@yahoo.com (Alan McKenney) wrote (abridged):
Only if you think that std::pair should attempt
to satisfy every possible need for an object with
two members.
Well, no; just more variations on the notion of "pair". I used that
example partly because pair and point are so similar - but they are
different and we wouldn't want to use the same class for both.
I think the original author used point partly because it is very simple.
I used pair because it is equally simple, to illustrate that even simple
things can need to change.
To continue to use your example, there are
many flavors of "constant." If the idea is
that "second" should be constant from the
moment the object is created, std::pair is
just fine: use std::pair<A, const B>
The idea was to avoid using unnecessary per-instance memory. So we were
looking for something like:
struct pair {
pair( int first ) : first_( first ) {}
int first() const { return first_; }
void set_first( int first ) { first_ = first; }
int second() const { return 0; }
private:
int first_;
};
In fact, I have _never_ been in a situation where I
wished I had used an accessor instead of making a variable
public.
I'm mildly surprised. I've needed to change a representation without
changing the interface many times. It's a major reason for using
interfaces.
-- Dave Harris, Nottingham, UK.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]