Re: Creating an object that is read from an input stream.
From: David White (no_at_email.provided)
Date: 12/14/03
- Next message: Thomas Wintschel: "Re: floating point round in C++"
- Previous message: Christof Krueger: "Design question: using malloc?"
- In reply to: Jason Heyes: "Re: Creating an object that is read from an input stream."
- Next in thread: Jason Heyes: "Re: Creating an object that is read from an input stream."
- Reply: Jason Heyes: "Re: Creating an object that is read from an input stream."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 15 Dec 2003 09:52:38 +1100
"Jason Heyes" <geoffhys@optusnet.com.au> wrote in message
news:3fdc2284$0$18388$afc38c87@news.optusnet.com.au...
> "David White" <no@email.provided> wrote in message
> news:EgyCb.5399$xm.155110@nasal.pacific.net.au...
> >
> > You can throw an exception if that happens.
> >
>
> Exceptions are annoying. I like streams better.
They aren't interchangeable.
> >
> > How can anyone use the Box class if you don't allow one to be
constructed?
> >
>
> The main function shows how to use the Box class. If you want the Box
class
> to do other things, then you had better extend it. For the purposes of
this
> discussion, however, that is not necessary.
>
> >
> > > The constructor is private and directly initialises the
> > > data members of Box.
> > >
> > > Obviously it would be a mistake to add a default constructor that
> > > initialises the data members to default values.
> >
> > Why? The default constructor can create a Box that's valid. And then the
> >> function can check
> > the validity of the left, right etc. values read from the stream and
fail
> if they aren't right.
> >
>
> Let me ask you something. Should every class have a default constructor?
No.
> Should the Box class have a default constructor?
I wouldn't find it useful without one.
> You say the default
> constructor can create a Box that is valid. But how do you decide on which
> Box to create?
Whatever you like. A "unit Box" - 0,1,1,0 - is the obvious default, just as
zero is the obvious default for type int (and is what you get is you write
int()).
> What I am saying is that sometimes it doesn't make sense to
> have a default constructor. Do you agree?
Yes, but not in this case. If you have a default constructor the stream code
is quite simple. Without one it's unnecessarily complicated.
Not having a default constructor can be a real pain sometimes. If you have a
Box as a member of another class and there is no default constructor, the
class is forced to initialize it somehow in its own constructor, even if it
won't know what values it's supposed to have until later. In general, if
there's a reasonable default, I'd have a default constructor. It just makes
life easier.
> >
> > I think I must not be getting something here. How is your Box class
> different from any
> > equivalent Rectangle class that might represent a window or some other
> useful concept, and could
> > be used all over the place in some applications?
> >
> > DW
> >
>
> I don't know what Rectangle class you are referring to.
For example, for a graphics library of mine I wrote two rectangle classes.
One is LogRect, or "logical rectangle" which represents the logical, or
integer, units on the graphics device on which drawing takes place. The
other is VirRect, or "virtual rectangle", which represents a floating-point
scale of the user's choice. All drawable objects are defined in virtual
units, which are later converted to logical units for drawing. Within the
library source code, the type 'LogRect' appears 623 times, and the type
'VirRect' appears 902 times. These objects are used everywhere. Using them
would have been extraordinarily difficult if they didn't have default
constructors. To give just one example, the base class of all drawable
objects has two VirRects and one LogRect. It is not known what values some
or all of these rectangles will have until after the drawable object is
created.
> The Box class is very simple.
It's the simple classes that tend to be used the most, and for which a
default constructor is most useful.
> You can read and write Boxes using streams and get the area of
> a Box. You cannot create a Box without reading it from a stream. Is that
so
> wrong?
If that's what suits your purpose, no, but it's not something I can imagine
ever wanting.
> If you want you can modify the class to have this:
>
> Box::Box(int l, int r, int u, int d) : left(l), right(r), up(u), down(d)
> {
> if (!validate(l, r, u, d))
> throw std::runtime_error("Invalid arguments");
> }
>
> It still doesn't solve my initial problem. How will you read your first
Box
> object from a stream?
Box b;
if(is >> b)
{
// whatever
}
DW
- Next message: Thomas Wintschel: "Re: floating point round in C++"
- Previous message: Christof Krueger: "Design question: using malloc?"
- In reply to: Jason Heyes: "Re: Creating an object that is read from an input stream."
- Next in thread: Jason Heyes: "Re: Creating an object that is read from an input stream."
- Reply: Jason Heyes: "Re: Creating an object that is read from an input stream."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|