initializing structs

J

Joe

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;

BOOL CTestControlDlg::OnInitDialog()
{
//Here I initialize a value
fontStuff fs;
fs.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.
 
D

David Lindauer

Joe said:
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.

hi,

I would put 'fs' in the class definition for CTestControlDlg, then
initialize it in the constructor... but I don't have enough experience
with MFC to know where you really should initialize it... the problem
with the declaration as it stands is that it goes away at the end of the
function body. If you are wondering where to put the declaration for
'fontstuff' you can put that in the class declaration as well unless it is
going to get a lot
of use outside this particular class.

Also, this is C++ and you don't *have* to use typedef on the struct, for
example try this declaration:

struct myfontStuff {
...
} ;

and you can still do:

myfontStuff myVariable ;

instead of

struct myfontStuff myVariable;

C++ differs from C in that you don't need the 'struct' and 'union'
keywords when using a structure tag to declare variables, although it is
certainly valid to use them if you want.

David
 
T

Thomas Matthews

Joe said:
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
 
J

John Harrison

Joe said:
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;

BOOL CTestControlDlg::OnInitDialog()
{
//Here I initialize a value
fontStuff fs;
fs.m_bFontItalic = false;
}

The problem is that this fontStuff struct is destroyed at the end of the
OnInitDialog function.

You should put 'fontStuff fs;' in the CTestControlDlg class and nowhere
else. That way it lives as long as the class does, which is what you want I
think.

john
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top