Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. OOP vs, MFC question...

OOP vs, MFC question...

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++visual-studiographicsdesign
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    largeinsd
    wrote on last edited by
    #1

    Hi, I am creating an MFC app as well as learning about design patterns and good coding practices, and so I've got a question for you whizzes... I am creating a class to control a dialog which fills in a DirectX materials structure: typedef struct _D3DMATERIAL9 { D3DCOLORVALUE Diffuse; D3DCOLORVALUE Ambient; D3DCOLORVALUE Specular; D3DCOLORVALUE Emissive; float Power; } D3DMATERIAL9; My question is thus: The class I am creating for this control should take a pointer to a struct (as above) as an argument for it's constructor. This is not a problem if the control is being used as a modal dialog: D3DMATERIAL9 Mat; CDX9MaterialDialog dlg(&Mat); INT_PTR nResponse = dlg.DoModal(); But when the dialog is being used as a child, I have to declare the class as a member of the parent dialog, right? How can I ensure that the my class is initialized with a valid pointer, when the parent dialog class will use the default no argument constructor for the class? I would like to make sure that the dialog is initialized with a pointer so that the dialog class is not responsible for creating (and deleting) a new material structure. Thanks - @LG

    M 1 Reply Last reply
    0
    • L largeinsd

      Hi, I am creating an MFC app as well as learning about design patterns and good coding practices, and so I've got a question for you whizzes... I am creating a class to control a dialog which fills in a DirectX materials structure: typedef struct _D3DMATERIAL9 { D3DCOLORVALUE Diffuse; D3DCOLORVALUE Ambient; D3DCOLORVALUE Specular; D3DCOLORVALUE Emissive; float Power; } D3DMATERIAL9; My question is thus: The class I am creating for this control should take a pointer to a struct (as above) as an argument for it's constructor. This is not a problem if the control is being used as a modal dialog: D3DMATERIAL9 Mat; CDX9MaterialDialog dlg(&Mat); INT_PTR nResponse = dlg.DoModal(); But when the dialog is being used as a child, I have to declare the class as a member of the parent dialog, right? How can I ensure that the my class is initialized with a valid pointer, when the parent dialog class will use the default no argument constructor for the class? I would like to make sure that the dialog is initialized with a pointer so that the dialog class is not responsible for creating (and deleting) a new material structure. Thanks - @LG

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      in the dialog, you will have a member pointer to the D3DMATERIAL9 structure, just be certain you initialize it to NULL in the constructure; and create a Set function to set the structure in the dialog. in pseudo code.

      class YourDialog
      {
      YourDialog();
      D3DMATERIAL9* m_pMaterial;
      //...

      void SetMaterial( D3DMATERIAL9* pMaterial) { ASSERT( pMaterial); m_pMaterial = pMaterial;};
      };

      YourDialog::YourDialog() : m_pMaterial(NULL)
      {
      }

      and now, it's your job to check that each time you access m_pMaterial it is not NULL.

      Maximilien Lincourt Your Head A Splode - Strong Bad

      W 1 Reply Last reply
      0
      • M Maximilien

        in the dialog, you will have a member pointer to the D3DMATERIAL9 structure, just be certain you initialize it to NULL in the constructure; and create a Set function to set the structure in the dialog. in pseudo code.

        class YourDialog
        {
        YourDialog();
        D3DMATERIAL9* m_pMaterial;
        //...

        void SetMaterial( D3DMATERIAL9* pMaterial) { ASSERT( pMaterial); m_pMaterial = pMaterial;};
        };

        YourDialog::YourDialog() : m_pMaterial(NULL)
        {
        }

        and now, it's your job to check that each time you access m_pMaterial it is not NULL.

        Maximilien Lincourt Your Head A Splode - Strong Bad

        W Offline
        W Offline
        William Wang
        wrote on last edited by
        #3

        it adds more coupling between YourDialog and clients, assume what does it happen if doing this: D3DMATERIAL9 *pMeterial = new D3DMATERIAL9 ; YourDialog dlg; dlg.SetMaterial(pMeterial); delete pMeterial; dlg.Dosomething(); //this function relies on m_pMaterial it undoubtedly uses an invalid pointer, I think the better way is either change the function name to make callers clear YourDialog instance is attached to D3DMATERIAL9 pointer or maitance a D3DMATERIAL9 object inside of YourDialog class. it's much easier to change function name to: YourDilaog::AttachMaterial(D3DMATERIAL9 *pMaterial)

        life is like a box of chocolate,you never know what you r going to get.

        L 1 Reply Last reply
        0
        • W William Wang

          it adds more coupling between YourDialog and clients, assume what does it happen if doing this: D3DMATERIAL9 *pMeterial = new D3DMATERIAL9 ; YourDialog dlg; dlg.SetMaterial(pMeterial); delete pMeterial; dlg.Dosomething(); //this function relies on m_pMaterial it undoubtedly uses an invalid pointer, I think the better way is either change the function name to make callers clear YourDialog instance is attached to D3DMATERIAL9 pointer or maitance a D3DMATERIAL9 object inside of YourDialog class. it's much easier to change function name to: YourDilaog::AttachMaterial(D3DMATERIAL9 *pMaterial)

          life is like a box of chocolate,you never know what you r going to get.

          L Offline
          L Offline
          largeinsd
          wrote on last edited by
          #4

          Thanks for the insights you guys. Will, I think you're right... in either case the only way for the dialog to be sure that the D3DMATERIAL9 structure is valid is if it maintains one locally and copies it's value to one supplied by a client on request. Sucks, 'cuz that will slow everything down a bit, but I can't see a way around it. -@LRG

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups