Typically, an enumerator is used in the collection of objects, but the enumerator is implemented primarily for the access control of the collection object, and we can use the CComEnumOnSTL template to define our own enumerator, which involves complex replication policy definitions and the definition of the collection method implementation.
If we do not use an enumerator and use only an array of interfaces, the collection object will have a relatively simple implementation, and this article will explore the implementation of this method and provide the complete source code.
The following is an example of a collection object implemented with an interface array for your reference:
| Collection Object creation class: Cvoxsys |
Interface: Ivoxsys |
| Collection Object class: Cvoxchs |
Interface: Ivoxchs |
| Child object class: Cvoxch |
Interface: Ivoxch |
First, about Cvoxch child objects:
To add a private member variable to the CVOXCH:
private:
BSTR m_bstrMessage;
Initialize the value of a member variable in the Cvoxch constructor:
CVoxCh()
{
m_bstrMessage = A2BSTR("No inited!");
}
Add BSTR type attribute for interface ivoxch: Message
To modify the Get_message function:
STDMETHODIMP CVoxCh::get_Message(BSTR *pVal)
{
*pVal = m_bstrMessage;
return S_OK;
}
Ii. about Cvoxchs Collection objects:
Add two private member variables to Cvoxchs:
private:
CComPtr<IVoxCh> m_arrpCh[160]; //子对象接口数组
long m_nChs; //实例子对象计数
Initialize the value of a member variable in the Cvoxchs constructor:
CVoxChs()
{
m_nChs = 0;
}