call null constructor from another constructor

Discussion in 'C++' started by Matt Graham, Feb 6, 2004.

  1. Matt Graham

    Matt Graham Guest

    Here's a subset of a class I'm trying to initialize:

    class uiForm {
    public:
    uiForm();
    uiForm( unsigned short );
    unsigned short m_method;
    unsigned short m_form_id;
    FormType *m_frmP;
    char *m_title;
    };

    I have this as my null constructor and the constructor below is passed
    an integer to set one of the parameters. But I want the rest of the
    data to be initialized the same as in the empty constructor. Now,
    what I'm wondering is if there is a way I can have integer constructor
    call the null constructor, and then I can initialize the one m_form_id
    value explicitly.

    uiForm::uiForm() :
    m_method( 1 ),
    m_form_id(),
    m_frmP(),
    m_title()
    {
    }

    uiForm::uiForm( unsigned short frm_id ) : m_form_id( frm_id )
    {
    }

    Thanks,
    Matt
    Matt Graham, Feb 6, 2004
    #1
    1. Advertising

  2. "Matt Graham" <> wrote...
    > Here's a subset of a class I'm trying to initialize:
    > [...]


    Is this a write-only newsgroup? Less that a week ago
    the influx of "how to call a constructor from another
    constructor" messages began. Have you seen any of
    them?

    No matter. The subject is covered in the FAQ. Please
    see Constructors section.
    Victor Bazarov, Feb 6, 2004
    #2
    1. Advertising

  3. Matt Graham wrote:

    > Here's a subset of a class I'm trying to initialize:
    >
    > class uiForm {

    private:
    > unsigned short m_method;
    > unsigned short m_form_id;
    > FormType *m_frmP;
    > char *m_title;
    > public:
    > uiForm(unsigned short);
    > uiForm(void);
    > };
    >
    > I have this
    >
    > uiForm::uiForm(): m_method(1),
    > m_form_id(0), m_frmP(0), m_title(0) { }
    >
    > as my [default] constructor and the [explicit] constructor below
    >
    > uiForm::uiForm(unsigned short frm_id): m_method(1),
    > m_form_id(frm_id), m_frmP(0), m_title(0) { }
    >
    > is passed an integer to set one of the parameters.
    > But I want the rest of the data to be initialized
    > the same as in the [default] constructor.
    > Now, what I'm wondering is if there is a way
    > I can have integer constructor call the [default] constructor
    > and then I can initialize the one m_form_id value explicitly.


    No.
    E. Robert Tisdale, Feb 6, 2004
    #3
  4. Matt Graham

    Matt Graham Guest

    Victor Bazarov wrote:
    > "Matt Graham" <> wrote...
    >
    >>Here's a subset of a class I'm trying to initialize:
    >>[...]

    >
    >
    > Is this a write-only newsgroup? Less that a week ago
    > the influx of "how to call a constructor from another
    > constructor" messages began. Have you seen any of
    > them?
    >
    > No matter. The subject is covered in the FAQ. Please
    > see Constructors section.


    Yeah, I knew I was going to get it for this one ;)
    I found that thread only a few minutes after posting. I had done some
    searching, but couldn't imagine that it would be right there in front of
    me like that. I'll have to look through the FAQ a little more carefully
    from now on. thanks
    Matt Graham, Feb 7, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,492
    Mike Schilling
    Mar 29, 2006
  2. Replies:
    0
    Views:
    2,718
  3. Andy
    Replies:
    9
    Views:
    494
    Pete Becker
    Sep 24, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,197
  5. Warren Tang
    Replies:
    9
    Views:
    661
    James Kanze
    Apr 15, 2008
Loading...

Share This Page