18.04.2015 Views

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

ArcGIS Engine Developer Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C++ APPLICATION PROGRAMMING INTERFACE<br />

C++ API versus Visual C++ API: Do not confuse<br />

the C++ API with the Visual C++ COM API. If<br />

developing only on Windows, the Visual C++<br />

COM API has advantages over the pure C++ API.<br />

The C++ API is primarily aimed at UNIX and<br />

cross-platform development.<br />

C++ is an object-oriented programming language that evolved in the mid-1980s<br />

from its predecessor, C. C++ is endowed with many features that give the language<br />

an unrivaled expressive power, such as object orientation with inheritance,<br />

operator overloading, virtual functions, templates, and a library of useful and<br />

often necessary functions called the Standard Template Library (STL). The C++<br />

language has been standardized by the International Organization for Standardization<br />

(ISO) and several influential national standards organizations.<br />

<strong>Developer</strong>s may consider using the <strong>ArcGIS</strong> C++ API, as opposed to one of the<br />

other APIs, for the following reasons:<br />

• Execution speed—C++ code typically executes faster than the equivalent<br />

Java, Visual Basic, C#, and VB.NET code.<br />

• Cross-platform compatibility—Visual Basic, Visual C++, VB.NET, and C# are<br />

currently used primarily on the Windows platform. C++ and Java are inherently<br />

more cross-platform.<br />

• Prior familiarity—If you already have a good deal of experience using the<br />

language, then C++ is a logical choice.<br />

This section is intended to serve two main purposes:<br />

1. To familiarize you with general C++ coding style and debugging.<br />

2. To provide an introduction to the <strong>ArcGIS</strong> C++ API, detailing specific usage<br />

requirements and recommendations for working with the ArcObjects programming<br />

platform.<br />

C++ DEVELOPMENT TECHNIQUES<br />

Smart types<br />

Smart types are objects that behave like types. They are C++ class implementations<br />

that encapsulate a data type, wrapping it with operators and functions that<br />

make working with the underlying type easier and less error prone. When these<br />

smart types encapsulate an interface pointer, they are referred to as smart pointers.<br />

Smart pointers work with the IUnknown interface to ensure that resource allocation<br />

and deallocation are correctly managed. They accomplish this by various<br />

functions, construct and destruct methods, and overloaded operators.<br />

Smart types can make the task of working with COM interfaces and data types<br />

easier, since many of the API calls are moved into a class implementation; however,<br />

they must be used with caution and never without a clear understanding of<br />

how they are interacting with the encapsulated data type.<br />

The smart types supplied with the C++ API consist of:<br />

• _com_ptr_t—This class encapsulates a COM interface pointer, creating a smart<br />

pointer.<br />

• CComBSTR—This class encapsulates the BSTR data type.<br />

• CComVariant—This class encapsulates the VARIANT data type.<br />

To define a smart pointer for an interface, you can use the macro<br />

_COM_SMARTPTR_TYPEDEF like this:<br />

_COM_SMARTPTR_TYPEDEF(IFoo, __uuidof(IFoo));<br />

Chapter 4 • <strong>Developer</strong> environments • 197

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!