12.6. Generic Interfaces in C++

Generic interfaces are the most basic of the generic constructs you'll run into with C++. At the same time, as you begin to look at mixing generics and templates (discussed later in this chapter), you'll find these generic interfaces taking on added significance. For now, though, take a look at how a generic interface is declared in C++:

generic <typename T>
interface class IDBObject {
    bool IsValid();
    T GetObject(int id);
    List<T>^ FindObjectsById(int id);
    List<T>^ FindObjectByName(String^ name);
    bool InsertObject(T dataObject);
    bool InsertObjects(List<T>^ dataObjects);
    T UpdateObject(T dataObject);
};

This interface declaration is almost identical to the syntax that was used to declare your generic class. You'll notice that references to the type parameter are sprinkled all throughout this interface. The only other distinguishing C++ characteristic is the addition of the handles for the List<T> and String reference types that are part of this interface declaration.

I should also point out the inclusion of the List<T> from the System::Collections::Generic namespace. C++ has full access to the types that are included in this library. Of course, within C++, using these types takes on more meaning because they compete with the STL types that provide similar functionality. Which containers you choose to use will likely have a lot to do with whether or not your code interacts with or is part of a managed solution.

Get Professional .NET 2.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.