I want a class between CDialog and CMyDialog with common code but How?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: I want a class between CDialog and CMyDialog with common code but How?

  1. #1
    Join Date
    Aug 2006
    Posts
    508

    I want a class between CDialog and CMyDialog with common code but How?

    I have several dialog classes which has a lot of common data but has differences in how each class handles the data. Now I want this common data to be defined in one parent class so each class doesn't have its own individual members but how can I do that given a dialog class is derived from CDialog in the first place? Is there any neat way other than multiple inheritance? I have never seen multiple inheritance used in any code and is discouraged by books..is that so?

    My situation is very simple to below but that member variables are a lot and more complicated:

    Suppose you have 4 dialogs each has its two integer member variables. The first one divides them, the 2nd on subtracts, the third multiply and 4th divides. Now I would want a base class which contain the two numbers that each class operates on but how do I define such a class so that I can derive each final dialog class from it? This will ideally sit between CDialog and CMyDialog in hierarchy but how to define it? All dialogs are direct derivatives of CDialog!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,106

    Re: I want a class between CDialog and CMyDialog with common code but How?

    Create your intermediate class. Change the inheritance on your other classes to derive from it, instead of CDialog.

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: I want a class between CDialog and CMyDialog with common code but How?

    I use custom base dialog classes in many of my applications. It's not really difficult to do (at least in Visual Studio 6) but does require some attention to detail.

    First create your base class from a a blank dialog template. MFC always adds an enum member to the dialog header like this:

    enum { IDD = IDD_BASE_DIALOG };

    Just delete this line as your base class will not have a fixed IDD (and you can delete the dialog template too). When you delete it you will find that your constructor will fail since IDD is undefined.
    Code:
    CBaseDlg::CBaseDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CBaseDlg::IDD, pParent)
    {
    }
    That's OK. Just change it to this:
    Code:
    CBaseDlg::CBaseDlg(UINT idd, CWnd* pParent /*=NULL*/)
    	: CDialog(idd, pParent)
    {
    }
    Of course, you will have to change the declaration in the header file too. But you should also add a default constructor that does nothing in the private section so that it will force you to pass the real IDD from the derived class.

    In other words, in your header file:

    Code:
    private:
    CBaseDlg::CBaseDlg() : CDialog((UINT)0, (CWnd*)0)
    {
        ASSERT(FALSE);   // No default constructor for base dialog
    }
    
    public:
    // Derived dialogs are required to call this constructor:
    CBaseDlg::CBaseDlg(UINT idd, CWnd* pParent /*=NULL*/);
    Now create a derived dialog like you usually do by designing a template and creating the files by deriving from CDialog. Go into the header file and add an include at the top for your base dialog.

    Change all instances of CDialog in the header file to CBaseDlg. Most likely will be only one - at the top of the class definition.

    Do the same thing in the C++ file EXCEPT for any instance enclosed in an AFX MACRO - leave those as CDialog.

    That's about all there is to it. There are some advanced issues to be aware of when using message maps, etc. but what I have outlined above will get you started.

    And if anyone knows of an easier way to do this I'd like to hear it.

  4. #4
    Join Date
    Mar 2001
    Posts
    2,366

    Re: I want a class between CDialog and CMyDialog with common code but How?

    It is also possible to eliminate resource files if those get in your way. Just use the dynamic dialog. For details see the link in my sig.
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center