Need help implementing a class which has NO base class 
Author Message
 Need help implementing a class which has NO base class

I added a class with no base class - let's call it CCalled.

How do I call one of its member functions from a dialog based class that I
created - lets call it CMyDialog?

In other words, what do I need in the include file for CMyDialog (and
CCalled) to be able to instantiated and call its public member functions -
besides adding the include file reference for the CCalled '.H' file.

The member functions are public, but do I have to use friend also? Or, do I
have to make the member functions I am trying to call 'virtual' in
CMyDialog?

Thank you for any suggestions you can provide.

Michael T.



Sat, 27 Jul 2002 03:00:00 GMT  
 Need help implementing a class which has NO base class
To instantiate CCalled, you can do 1 of 4 things:

1. Define an object of this class on the stack, as in:

    #include "Called.h"
    void CMyDialog::OnInitialUpdate()
    {
         CCalled called;
         called.DoSomething();
        ....
    }

In this case the object disappears when the function returns;

2. Define the object as a member of CMyDialog, in the class definition in
MyDialog.h:

    #include "Called.h"
    class CMyDialog : public CDialog
    {
         .....
    private:
        CCalled  m_called;
    };

If you do this, m_called is instantiated when CMyDialog is instantiated, and
you can use m_called and its public members anywhere in CMyDialog member
functions. No need to clean it up.

3. Allocate the CCalled object on the heap, as in

    #include "Called.h"
    void CMyDialog::OnInitialUpdate()
    {
         CCalled* pCalled = new CCalled;
         pCalled->DoSomething();
         delete pCalled;
        ....
    }

4. If you want to access this object anywhere in the dialog code, and the
CCalled constructor takes parameters, you might want to combine 2 & 3 above:

    #include "Called.h"
    class CMyDialog : public CDialog
    {
         .....
    private:
        CCalled*  m_pCalled;    // note: this is a pointer, not an object
    };

    // initialize the CCalled member pointer to null in the CMyDialog
constructor
    CMyDialog::CMyDialog(CWnd* pParent /*=NULL*/)
            : CDialog(CMyDialog::IDD, pParent),
              m_pCalled( NULL )
    {
    }

    // allocate an object and store in the member variable
    void CMyDialog::OnInitialUpdate()
    {
         m_pCalled = new CCalled( param1, param2 );    // pass in any
required info
         m_pCalled->DoSomething();
        ....
    }

    // delete the allocation in the CMyDialog destructor (or in the
OnDestroy message handler)
    CMyDialog::~CMyDialog()
    {
        if ( m_pCalled )
            delete m_pCalled;
    }


Quote:
> I added a class with no base class - let's call it CCalled.

> How do I call one of its member functions from a dialog based class that I
> created - lets call it CMyDialog?

> In other words, what do I need in the include file for CMyDialog (and
> CCalled) to be able to instantiated and call its public member functions -
> besides adding the include file reference for the CCalled '.H' file.

> The member functions are public, but do I have to use friend also? Or, do
I
> have to make the member functions I am trying to call 'virtual' in
> CMyDialog?

> Thank you for any suggestions you can provide.

> Michael T.



Sat, 27 Jul 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Protected member in a base class of a base class

2. class class-name: base-class : interface

3. Trying to add a class with no base class in Class Wizard

4. needing help learning how to implement this as a class

5. Some help needed with abstract base classes

6. Need help creating/reusing a DLL based on a class

7. Help needed in creating new classes - not based on anything MFC

8. How to implement MDI with different base classes?

9. Implementing Error Reporting in an ActiveX Control based on the MFC class COleControl

10. need help making class object visible to rest of class functions

11. Need help in docking CView class and CDialog view class to a MDI frame

12. Internal Compiler Error while deriving C# class from a MC++ class implementing a C# interface

 

 
Powered by phpBB® Forum Software