Joe wrote:
Hi,
I have been struggling with this issue for a couple of days and would
like to know if some can give me a pointer.
I want to initialize a struct with default values and depending on the
value returned from a fontdialog box, I want to update the values of
the struct.
I have included a code snippet below which I think will make my
question clearer.
typedef struct _fontStuff
{
bool m_bFontItalic;
…..
}fontStuff;
In C++, the typedef and tag label are not necessary.
The above could be stated as:
struct fontStuff
{
bool m_bFontItalic;
};
BOOL CTestControlDlg::OnInitDialog()
{
//Here I initialize a value
fontStuff fs;
fs.m_bFontItalic = false;
}
The best method for initializing an object of fontStuff
is to have a default constructor:
struct fontStuff
{
fontStuff()
: m_bFontItalic(false), /* ... */
{
/* any complex default initialization goes here */
}
bool m_bFontItalic = false;
};
void CTestControlDlg::OnButton1()
{
CFontDialog fd;
//rather than using another variable here I would like to
update //fs.m_bFontItalic that I initially set.
bool IsItalic;
if(fd.DoModal() == IDOK)
{
IsItalic = fd.IsItalic;
}
}
Thanks in advance for any help.
Another idea is to pass the dialog box to the fontStuff
so that the fontStuff can set its data members from the
dialog box:
void CTestControlDlg::OnButton1()
{
fontStuff fs;
fs(*this); // Let fontStuff assign its members based
// on the dialog box.
/* ... */
}
By the way, you don't have to follow Microsoft's naming
convention for your own classes. For example, you don't
need to prefix the class names with 'C'.
Also, I don't suggest you mix member prefixing with
Hungarian notation. The identifier "m_bFontItalic"
becomes difficult to descipher: member, byte storage
for Font Italic. Or is that member, boolean storage
for Font Italic? [Also, if you change the identifier's
type, are you going to rename every instance of that
identifier throughout the all the source code?]
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library