views:

49

answers:

3

I am learning c++ and I am trying to better understand it. I was reading the msdn documents on how to use xml lite. it said that I must use a class that implements the IStream interface. It said to declare and instantiate my class that extends Istream and use CComPtr when declaring the varible. then it showed me the following.

CComPtr<IStream> pFileStream;
CComPtr<IXmlReader> pReader;

I am a tad bit confused. if CComptr is used to pull the xml. why do I have to extend . Why not just have CComptr already implement IStream and just call CComptr. Or does CComptr already have IStream and the only way for istream to be effective is to extend like above???

+1  A: 

CComPtr<> is a smart pointer used to automate managing the object lifetime. It is more or less the same as the Interface* where Interface is the CComPtr<> template parameter (IStream* or IXmlReader* in this example), but provides some additional features that don't influence how the object pointed to function.

So CComPtr<IStream> has an IStream* inside and an overloaded operator ->() which redirects calls to that IStream*. The same applies to CComPtr<IXmlReader> - it has IXmlReader* inside.

sharptooth
+1  A: 

if CComptr is used to pull the xml. why do I have to extend . Why not just have CComptr already implement IStream and just call CComptr?
IStream is an interface -- saying "I want some class which implements this interface" does not tell how you want to actually get the data. CComPtr is only a pointer to a coclass which implements an interface -- it does not actually implement any interface itself.

Is it possible to implment a COM interface without creating a custom interface?
I'm not 100% positive here, but I don't believe you need to implement an interface. You do however need to implement the interface itself in a coclass.

Billy ONeal
You're right. It's quite common in fact. For instance, all Shell Extensions implement the COM interfaces useb by Explorer.exe. Few add custom interfaces; Explorer isn't going to use them so they'd be for internal use only.
MSalters
Ok, I am use to languages where if a class implements a interface. you can just call the class. You don't have to do class<interface>. I am under the impression that in c++, in order for the compiler to take notice of the interface you must call it like class<interface>. correct me if I am wrong.
numerical25
@numerical25 It's not a problem of the C++ language. It's COM's. COM is not part of C++. It's a Microsoft convention at the binary level. You can implement COM objects in C as well. http://en.wikipedia.org/wiki/Component_Object_Model
sergiom
@numerical25: To put the previous comment another way -- you're mixing up C++ classes and COM CoClasses. The two are completely unrelated. A CoClass implements a colection of interfaces, and can be written in any COM supported language. Even languages like C which have no concept of classes or objects.
Billy ONeal
ok. well I guess I need to do more research on com
numerical25
A: 

This question requires a complex answer.

COM interfaces are not part of C++ language. They can be implemented using different languages. C++ is just one of them.

Every COM interface inherits from the IUnknown interface, that implements QueryInterface() AddRef() and Release() methods

QueryInterface() must be used to request the COM object interfaces. Since every COM interface inherits from IUnknown, it can be called on any interface.

AddRef() and Release() must be called to manage the object lifetime.

CComPtr<> is a template class, implemented in Microsoft ATL library, to wrap any COM interface, that automatically calls QueryInterface(), AddRef() and Release() when needed.

In your example, CComPtr pFileStream can be used to access IStream interface members of an object.

http://msdn.microsoft.com/en-us/library/ezzw7k98%28VS.80%29.aspx

sergiom