initializing structs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    #1

    initializing structs

    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_bFontItali c = false;
    }

    void CTestControlDlg ::OnButton1()
    {
    CFontDialog fd;
    //rather than using another variable here I would like to
    update //fs.m_bFontItali c that I initially set.
    bool IsItalic;

    if(fd.DoModal() == IDOK)
    {
    IsItalic = fd.IsItalic;
    }
    }

    Thanks in advance for any help.
  • David Lindauer

    #2
    Re: initializing structs


    Joe wrote:
    [color=blue]
    > 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.
    >[/color]

    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



    Comment

    • Thomas Matthews

      #3
      Re: initializing structs

      Joe wrote:[color=blue]
      > 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;[/color]
      In C++, the typedef and tag label are not necessary.
      The above could be stated as:
      struct fontStuff
      {
      bool m_bFontItalic;
      };

      [color=blue]
      >
      > BOOL CTestControlDlg ::OnInitDialog( )
      > {
      > //Here I initialize a value
      > fontStuff fs;
      > fs.m_bFontItali c = false;
      > }[/color]
      The best method for initializing an object of fontStuff
      is to have a default constructor:
      struct fontStuff
      {
      fontStuff()
      : m_bFontItalic(f alse), /* ... */
      {
      /* any complex default initialization goes here */
      }
      bool m_bFontItalic = false;
      };

      [color=blue]
      >
      > void CTestControlDlg ::OnButton1()
      > {
      > CFontDialog fd;
      > //rather than using another variable here I would like to
      > update //fs.m_bFontItali c that I initially set.
      > bool IsItalic;
      >
      > if(fd.DoModal() == IDOK)
      > {
      > IsItalic = fd.IsItalic;
      > }
      > }
      >
      > Thanks in advance for any help.[/color]

      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_bFontIta lic"
      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:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book
      http://www.sgi.com/tech/stl -- Standard Template Library

      Comment

      • John Harrison

        #4
        Re: initializing structs


        "Joe" <pimsor@hotmail .com> wrote in message
        news:272c8c59.0 410020628.6c655 8b1@posting.goo gle.com...[color=blue]
        > 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_bFontItali c = false;
        > }[/color]

        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


        Comment

        Working...