Replacing the Collection Classes

The Standard Template Library

The C# collection classes that you are familiar with can generally be replaced with an equivalent from the C++ Standard Template Library (STL). The STL classes are all pretty consistent, but the first time you use one you are bound to find the syntax a bit fiendish. Perhaps the first thing you’re likely to need is a dynamic sequential list of some sort – in the stl, this is a list template class. I’ll use the list as an example here, but be sure to do a search on ‘STL containers’ to make sure you are aware of the full range available to you. For example, the typical replacement for a C# array would be a vector and the equivalent of a dictionary is a map.

Creating a list

Before you can use the list class you must, unsurprisingly, include the appropriate header it in your code:

#include <list>

Then you use standard template syntax to create your list, in this case I have created a list of IUpdateable pointers:

std::list<IUpdateable*> m_updateables;

Read more: Replacing the Collection Classes

Interfaces in C++

How do I use a C# like interface based design pattern in C++?

If you are planning to deploy to Windows only and you are using Visual Studio, you can use Microsoft's __interface extension. Essentially this extra keyword enforces the structure and usage described below. For more info see this MSDN article.

For cross-platform use, you need to use the standard C++ constructs to roll your own interfaces. This is done by declaring a class that is abstract and contains only pure virtual declarations - that is the methods must be implemented in any derived class.

Read more: Interfaces in C++

You are here: Home C++ Moving from C#