Re: Copy constructor needed I think, maybe some op= as well?

From: Darren Grant (dg_6_at_hotmail.com)
Date: 03/08/04

  • Next message: Dennis Kernighan: "Fortran from C++"
    Date: Mon, 08 Mar 2004 14:23:30 -0000
    
    

    > Well. There are 2 ways.
    > The first, and this is the one you should try to go unless there is a
    > good
    > reason not to, is:
    > avoid dynamic allocations at all.
    >
    > In your case that means: Why on earth do you allocate the Viewport object
    > in RadioButton dynamically? As far as I can see there is no good reason
    > to do so.
    >
    > Why not simply?
    >
    > class RadioButton : public Control {
    >
    > public:
    > RadioButton() {}
    > RadioButton(Point loc, string label, int ident, GLfloat *colour)
    > : Control(loc, Point(2, 2), Point(10, 10), colour), inner_button( border
    > + Point(2, 2),
    > Point(0, 0), Point(6, 6))),
    > label(string(label)), selected(false), ident(ident) { }
    >
    > // ...
    >
    > private:
    > Viewport inner_button;
    > string label;
    > bool selected;
    > int ident;
    > };
    >
    > and all of your problems with RadioButton would go away: No destructor
    > needed, no copy constructor needed, no assignment operator needed, no
    > need to correct the (in your version) wrong default constructor (What
    > happens to the pointer inner_button, if the default constructor is used?)

    Damnit. I thought it was preferable to do things dynamically.

    The thing is, I was under the impression that without using pointers, every
    object is going to get initialised twice; eg

    Viewport inner_button;

    calls the default constructor for Viewport, and then you assign it at a
    later
    date, in

    inner_button( border + Point(2, 2)...

    which I thought was a bad thing because it's wasting cycles and memory
    transfers.

    What about for a game, when you might create and destroy large numbers of
    objects
    every second? Is it still OK to do this?

    Also because when I had it like this, I had lots of empty constructors;

    Viewport() { }

    which just didn't seem quite right.

    I guess I was a little off the mark?

    Darren


  • Next message: Dennis Kernighan: "Fortran from C++"
  • Quantcast