In this lesson, we will add internal member variables and methods to
CPhBookObj
to support storing and managing
PhRec
structures. The first step is to add a constant for the maximum number of phone records. Open PhBookObj.h and add
#define MAX_RECS 32
after
#include resource.h
.
After we have added the constant for the maximum number of phone records, we have to:
- Add an array of
PhRecs
to CPhBookObj
- Add a member variable,
m_numrecs
, to hold the number of records currently stored in m_PhRecs
- Add member variable
m_currec
to hold the value of the CurRec
property; a value of -1
indicates that CurRec
is not set
Finally, once we have added the member variables, we have to initialize
m_numrecs
and
m_currec
in the constructor of
CPhBookObj
in PhBookObj.h.
CPhBookObj() : m_numrecs(0), m_currec(-1)
{}
Changes to CPhBookObj are reflected in PhBookObj.h.
class CPhBookObj :
public IReadPhBook,
public IManagePhBook,
public CComObjectRoot,
public CComCoClass<CPhBookObj,&CLSID_PhBookObj>
{
public:
CPhBookObj() : m_numrecs(0), m_currec(-1) {}
BEGIN_COM_MAP(CPhBookObj)
COM_INTERFACE_ENTRY(IReadPhBook)
COM_INTERFACE_ENTRY(IManagePhBook)
END_COM_MAP()
DECLARE_REGISTRY_RESOURCEID(IDR_PhBookObj)
public:
STDMETHOD(DeletePhoneRec)(/*[out]*/ BOOL *pOK);
STDMETHOD(AddPhoneRec)(/*[in]*/ PhRec *pPhRec,
/*[out]*/ BOOL *pOK);
STDMETHOD(GetPhoneRec)(/*[in]*/ PhRec *pPhRec,
/*[out]*/ BOOL *pOK);
STDMETHOD(get_NumRecs)(/*[out, retval]*/
long *pVal);
STDMETHOD(get_MaxRecs)(/*[out, retval]*/
long *pVal);
STDMETHOD(get_CurRec)(/*[out, retval]*/
long *pVal);
STDMETHOD(put_CurRec)(/*[in]*/ long newVal);
protected:
long m_currec;
long m_numrecs;
PhRec m_PhRecs[MAX_RECS];
};