473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

initializing structs

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.
Jul 22 '05 #1
3 2625

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.


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

Jul 22 '05 #2
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

Jul 22 '05 #3

"Joe" <pi****@hotmail.com> wrote in message
news:27**************************@posting.google.c om...
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mark fine | last post by:
////////////// snippet //////////////// struct t { static struct { } a; }; struct { } t::a; int main(void) {
7
by: Chandrashekar Tippur | last post by:
All, I need to initialize an array of structures but I don't know how may elements are there. I tried to malloc the array but I am not sure how to initialize them. Snippet: struct myarray{...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
4
by: ccdrbrg | last post by:
I am trying to initialize an arrary of pointers to structs with constants. Sample code: struct mystruct { char *text; int number; };
7
by: Eric Johannsen | last post by:
Hi, My C# code is calling VB6 code, which expects all (fixed-length) strings to be padded with spaces. The strings are contained with a struct, something like this (attributes to simulate...
5
by: ScottM | last post by:
I expect this has been covered somewhere, but I can't find it. Given: class A { public: A() {...does stuff...} }; struct S { A a; };
4
by: John | last post by:
I can do this double T = { {1,3,4},{7,8,9}}; but not this double T; T = { { a,b,c} , {d,e,f} }
5
by: 2b|!2b==? | last post by:
I have a class that contains C structs as member variables. By C structs, I mean they cannot have ctors/dtors because they have C linkage (extern "C"). For eg: MyClass { //Impl private:
14
by: Carramba | last post by:
Hi! I have a big struct and I want to initialize it at once, but I get parse error before "{" compiler error. can't it by done? #include <stdio.h> #include <stdlib.h> typedef struct{ int b;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.