Derived Constructor Question

This is a discussion on Derived Constructor Question within the C++ Programming forums, part of the General Programming Boards category; Is there a cleaner way to call a class's constructor when declaring a class derived from it without having to ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    11

    Derived Constructor Question

    Is there a cleaner way to call a class's constructor when declaring a class derived from it without having to redeclare all its parameters?

    So far this is the most suitable solution I've figured out:
    Code:
    class Widget
      Widget(int x, int y, int w, int h) { ... }
      Widget(Widget* _Widget) { x = _Widget->x; ... }
    
    class w_Button:public Widget
      w_Button(string title, int type, Widget _Widget):Widget(_Widget) { ... }
    As opposed to:
    Code:
      w_Button(string title, int type, int _x, int _y, int _w, int _h):Widget(_x,_y,_w,_h) { ... }
    This way, with c++0x I can just go:

    Code:
    new w_Button("Test", BUTTON_TEST, {10,10,32,10});
    So I guess I'm wondering if there's anything sneaky built into either c++ or c++0x that cuts out the need to make a temporary instance of the base class and send a second set of parameters directly to the base constructor.

    Or some other solution I'm completely overlooking.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps you could reduce the number of arguments with a Rectangle class (x, y, w, z)? Otherwise, perhaps the Widget constructor has other side-effects besides just setting data values?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    I'd pass title by const-reference there. However, does the title need to be set from the beginning? You could make it the last parameter and default it to empty string, and then provide a SetTitle method. That would give you one less argument sometimes.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Quote Originally Posted by Jimage View Post
    Is there a cleaner way to call a class's constructor when declaring a class derived from it without having to redeclare all its parameters?

    So far this is the most suitable solution I've figured out:
    Code:
    class Widget
      Widget(int x, int y, int w, int h) { ... }
      Widget(Widget* _Widget) { x = _Widget->x; ... }
    
    class w_Button:public Widget
      w_Button(string title, int type, Widget _Widget):Widget(_Widget) { ... }
    As opposed to:
    Code:
      w_Button(string title, int type, int _x, int _y, int _w, int _h):Widget(_x,_y,_w,_h) { ... }
    This way, with c++0x I can just go:

    Code:
    new w_Button("Test", BUTTON_TEST, {10,10,32,10});
    So I guess I'm wondering if there's anything sneaky built into either c++ or c++0x that cuts out the need to make a temporary instance of the base class and send a second set of parameters directly to the base constructor.

    Or some other solution I'm completely overlooking.
    Define a class/structure to hold the parameters and just pass that down the chain. Otherwise, I can't think of a very practical way to do that, honestly...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comments on value_ptr class?
    By Sebastiani in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2009, 03:16 PM
  2. List of Functors
    By golfinguy4 in forum C++ Programming
    Replies: 13
    Last Post: 09-29-2009, 01:53 PM
  3. Friends design
    By Elysia in forum C++ Programming
    Replies: 22
    Last Post: 07-30-2009, 01:33 PM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21