Table of contents
1.
Introduction
2.
Generating a New Simple Object
3.
Editing the IDL File
4.
Creating Typedefs for Storage and Exposure
5.
Creating Typedefs for Copy Policy Classes
6.
Creating Typedefs for Enumeration and Collection
7.
Editing the Wizard-Generated Code
8.
Adding Code to Populate the Collection
9.
Frequently Asked Questions
9.1.
What is typedef?
9.2.
What is CComEnumOnSTL Class?
9.3.
What is an IDL file?
10.
Conclusion
Last Updated: Aug 13, 2025
Medium

Implementing a C++ Standard Library Based Collection

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninjas!! Welcome to another article on Advanced Template Library. Today we will learn about Implementing a C++ Standard Library Based Collection. The Advanced Template Library comprises all template-based C++ classes. The Advanced Template library had a predefined ICollectionOnSTLImpl interface. The ICollectionOnSTLImp allows to implement C++ Standard Library based collection interface efficiently.

Implementing a C++ Standard Library Based Collection Image

Without further ado, let's look into the details of Implementing a C++ Standard Library Based Collection.

Generating a New Simple Object

The first step is to generate a new simple object. Follow the following steps to generate a new object:

  • Uncheck the Attributes box under the Application Settings.
     
  • Create a new Project.
     
  • Create a simple object named Words using ATL Add Simple Object Wizard and ATL Add Class dialog box.
     
  • The dual interface named IWords will be automatically generated.
     
  • Create the objects of the class as required. They are used to represent a collection of words.

Editing the IDL File

The next step is to edit the IDL file. The IDL file is used to create header and proxy files.

edit image

We have to add three properties to the IDL file. These properties will convert the interface into a read-only collection interface. You can have a look over the IDL file below:

Code:

[
   object,
   uuid(96842514-9E4C-2804-AB98-030098666741),
   dual, // dual interface
   nonextensible, // nonextensible attribute
   pointer_default(unique)
]
interface IWords : IDispatch
{
   [id(DISPID_NEWENUM), propget] // DISPID enables automation clients to use the interface.
   HRESULT _NewEnum([out, retval] IUnknown** ppUnk);

   [id(DISPID_VALUE), propget]                              
   HRESULT Item([in] long Index, [out, retval] BSTR* Value) 

   [id(0x00000001), propget]                                
   HRESULT Count([out, retval] long* Value);

};
You can also try this code with Online C++ Compiler
Run Code


Explanation:

Here, we have used the DISPID_VALUE for representing the id. You can give any value to the DISPID_VALUE. This will be used as the default property for the collections. This value is used to access the different methods of the class.

Creating Typedefs for Storage and Exposure

The next step is to decide the storage structure of the data. This is totally based on the user's requirement. You can store the data in integer, string, arrays, vectors, etc. This data will be exposed to the enumerator. 

Example:

typedef std::vector< std::int > CNumber;
typedef BSTR CType;
typedef IWords CInterface;
typedef VARIANT EVariant;
typedef IEnumVARIANT EInterface;
You can also try this code with Online C++ Compiler
Run Code


Explanation:

Here, we have created typedef for the storage of data. You can use other data types for naming the collections. The collection interface uses BSTR for exposing data. We have used IEnumVARIANT class as the enumerator. This class comprises all required methods for enumerating objects. It is a a user-defined data type containing integral constants.

Creating Typedefs for Copy Policy Classes

The typedef will provide all the required information in other classes also. These typedefs will be used by the enumerator and collection.

Thinking Image

 Take a look at the following code:

Code:

typedef VCUE::GenericCopy<EVariant, CNumber::value_type> EnumeratorCopy;
typedef VCUE::GenericCopy< CType, CNumber::value_type> CollectionCopy;
You can also try this code with Online C++ Compiler
Run Code


Explanation:

We had created a Generic copy of Enumerator and Collection. These are used to copy objects without duplicates from one class to another.  

Creating Typedefs for Enumeration and Collection

The code template requires CComEnumOnSTL and ICollectionOnSTLImpl classes. These classes provide methods for collection class. These classes can be created using as shown:

Code:

typedef CComEnumOnSTL< EInterface, &__uuidof(EInterface), EVariant, EnumeratorCopy, CNumber > Enumerator_Type
typedef ICollectionOnSTLImpl< CInterface, CType, CType, CollectionCopy, EType > Collection_Type;
You can also try this code with Online C++ Compiler
Run Code

 

Explanation:

In the above code snippet, we had created Typedefs for Enumeration and Collection class. Now we simply need to use Collection_Type for implementing ICollectionOnSTLImpl class and Enumerator_Type for implementing CComEnumOnSTL class. 

Editing the Wizard-Generated Code

The Wizard generated code contains the IWords interface. Implementing a Standard Library Based Collection requires CWords Interface. The ICollectionOnSTLImpl class contains the CWords interface. We had earlier created the alias of ICollectionOnSTLImpl class as Collection_Type. Edit the Wizard-Generated Code as:

Code:

class ATL_NO_VTABLE CWords :
   public CComObjectRootEx<CComSingleThreadModel>,
   public CComCoClass<CWords, &CLSID_Words>,
   public IDispatchImpl<Collection_Type, &IID_IWords, &LIBID_NVC_ATL_COMLib, 1, 0>
	{
		public:
		DECLARE_REGISTRY_RESOURCEID(IDR_WORDS)

		BEGIN_COM_MAP(CWords)
   		COM_INTERFACE_ENTRY(IWords)
   		COM_INTERFACE_ENTRY(IDispatch)
		END_COM_MAP()

		//Your code
	}
You can also try this code with Online C++ Compiler
Run Code


Explanation:

The Wizard Generated code comes with the predefined IWords Interface. For implementing a Standard Library Based Collection requires CWords Interface we require the CWords interface. We have used the Collection_Type typedef for using the ICollectionOnSTLImpl class.

Adding Code to Populate the Collection

The final step is to populate the collection. You can add a few words to the constructor of the class. These words can be any word of your choice. The collection is populated using the m_coll member of the class. You can have a look at the following code snippet:

Code:

CWords()
{
    m_coll.push_back("Coding");
    m_coll.push_back("Ninjas");
}
You can also try this code with Online C++ Compiler
Run Code

Frequently Asked Questions

What is typedef?

The typedef is a reserved keyword used for creating alias of the data types or predefined classes.

What is CComEnumOnSTL Class?

The CComEnumOnSTL class defines a COM enumerator object. This object is created for the C++ Standard Library collection.

What is an IDL file?

The IDL (Interface Definition) file contains information of the header and proxy files used in the interface.

Conclusion

In this article, we have discussed the details of Implementing a C++ Standard Library Based Collection.

We hope that the blog has helped you enhance your knowledge regarding Implementing a C++ Standard Library Based Collection. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blogs to help other ninjas grow. Happy Coding!!

Live masterclass