FAQ 34.21 What are the major differences between COM and C?

FAQ 34.21 What are the major differences between COM and C++?

graphics/new_icon.gif

COM is a component standard while C++ is an object-oriented language, and even though they have much in common, they really do have different roles in life.

As a programming language, C++ defines classes and objects, defines a complete syntax and semantics, and has a compiler that translates source text into executable code. In addition, C++ has a relatively small runtime system and restricts its view of the world to the objects in a single process.

As a component standard, COM defines classes and objects, has a minimal syntax and a minimal compiler (the MIDL compiler) for defining interfaces, defines an architecture for creating, locating, and accessing objects running in different processes, defines interfaces for marshaling parameters when calls are made to a different process (see FAQ 34.15 for more information regarding marshaling), and defines several threading models.

Therefore COM and C++ are completely different in their scope, but fortunately, they are compatible technologies: COM is well suited to defining components and interfaces, and C++ provides a rich language for implementing components and classes.

C++ FEATURE

COM FEATURE

A C++ class name is an identifier.

A COM class name is a CLSID or a 128-bit number.

C++ classes are not required to support a common interface.

COM classes are required to support the IUnknown interface.

C++ classes have constructors that are automatically called when objects are created.

COM classes do not have constructors (although the language used for implementing a COM class may provide a mechanism for executing an initialization routine when instances of the class are created).

C++ classes have destructors that are automatically called when objects are destroyed.

COM classes do not have destructors (although the language used for implementing a COM class may provide a mechanism for executing a finalization routine when instances of the class are destroyed).

A C++ class can declare pure virtual functions, virtual functions, nonvirtual functions, overloaded functions, static functions, data members, and friends.

A COM interface declares pure virtual functions only.

A C++ class represents an interface, although the interface does not have a distinct and explicit name or identifier.

A COM interface has an explicit identifier (IID).

A C++ class normally has one virtual function table that contains pointers to all the virtual functions defined by the object.

A COM class can be implemented using a separate virtual function table for each interface.

C++ callers can safely navigate to other interfaces using dynamic_cast.

COM callers can safely navigate to other interfaces using QueryInterface.

C++ callers can use new to create objects; this mechanism ends up calling the class's constructor. C++ callers can also create objects in a local scope.

COM callers typically use CoCreateInstance to create objects; this mechanism ends up calling the IClassFactory interface, which must be implemented for every COM class. COM callers can also directly call the CreateInstance method of the IClassFactory interface.

C++ callers use delete to destroy objects created using new; objects in a local scope are destroyed automatically.

COM callers do not destroy COM objects directly; instead they call Release() when they no longer need an interface pointer, and the COM object destroys itself.

When a caller creates a C++ object using new, the caller gets a pointer to the object.

When a caller creates a COM object, the caller gets a pointer to one of the object's interfaces; however, it never gets a pointer to the object no such pointer exists.

Error handling is normally accomplished using throw and catch; Return codes are supported but are undesirable.

Error handling is required to use return codes.

C++ supports both reference semantics and value semantics.

COM supports reference semantics only.

C++ does not define a meta data facility

Type libraries contain meta data.

C++ defines features such as templates, overloaded functions, operator overloading, default parameters, namespaces, try/catch exception handling, const declarations, references, private inheritance, protected inheritance, etc.

These features are not part of COM but they can be used by C++ programs that implement COM interfaces and COM classes.

C++ supports interface inheritance.

COM supports interface inheritance.

C++ supports multiple inheritance.

COM supports only single inheritance (although there are several techniques for building COM classes that implement multiple interfaces).

C++ supports implementation inheritance.

COM does not support implementation inheritance; however, COM does not prevent an implementation of a COM class (using C++, for example) from using implementation inheritance.

C++ does not use a system registry.

COM uses a system registry for storing static information about classes, interfaces, and type libraries.

C++ classes and namespaces reduce name clashes.

COM GUIDs eliminate name clashes.

C++ does not define any mechanisms for creating objects in other processes.

COM defines an architecture for creating out-of-process objects (both on the local machine and on remote machines).

C++ does not define any mechanisms to support communication between objects running in different processes.

COM defines an architecture for invoking methods on out-of-process objects, marshaling parameters from the caller to the object, and marshaling return values from the object to the caller.

C++ does not define a threading model

COM defines several threading models for governing how COM objects execute.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net