call null constructor from another constructor

M

Matt Graham

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
 
V

Victor Bazarov

Matt Graham said:
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.
 
E

E. Robert Tisdale

Matt said:
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.
 
M

Matt Graham

Victor said:
Matt Graham said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top