
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.