OOP vs, MFC question...
-
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
-
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
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
-
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
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.
-
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.
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